如何在spec2中使用mockito定义自定义参数匹配器?

2024-04-19

我想验证业务逻辑是否通过预期user反对 dao,但我不知道如何为其编写自定义参数匹配器。

"user" should {
    "be saved" in {
        val dao = new UserDao()
        dao.save(any[User]) returns mock[User]

        runMyBusinessLogic();

        val expectedUser = new User("Freewind", 123.23234)
        there was one(dao).save(mymatcher(expectedUser));
    }
 }

The User class:

case class User(name:String, random: Double)

其中包含一个double领域,我需要对其进行一些特殊的比较。

The mymatcher是我要定义的匹配器:

def mymatcher(expected: User) = ??? {
    // compare `name` and `random`
}

但我不知道该怎么做spec2,并找不到任何有用的文档。有什么帮助吗?


我使用 beLike 匹配器。像这样:

one(daoMock).insert { beLike[MyEntity] { case t:Entity => {
  t.summary mustEqual "Summary"
  t.description mustEqual "Description"
}}}

在 beLike 匹配器中,您可以使用普通值匹配器。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在spec2中使用mockito定义自定义参数匹配器? 的相关文章

随机推荐