Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a custom argument matcher with mockito in spec2?

I want to verify if the business logic passes the expected user object to dao, but I can't figure how to write an custom argument matcher for it.

"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)

Which contains a double field, that I need to do some special comparison for it.

The mymatcher is the matcher I want to define:

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

But I don't know how to do it in spec2, and can't find any useful documents. Any helps?

like image 607
Freewind Avatar asked Oct 22 '25 13:10

Freewind


1 Answers

I use beLike matcher. Like this:

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

Inside beLike matcher you could use ordinary value matchers.

like image 71
Mike G. Avatar answered Oct 24 '25 03:10

Mike G.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!