Is it recommended to mock concrete class?

后端 未结 5 2037
粉色の甜心
粉色の甜心 2020-12-02 10:09

Most of the examples given in mocking framework website is to mock Interface. Let say NSubstitute that I\'m currently using, all their mocking examples is to mock interface.

5条回答
  •  感情败类
    2020-12-02 10:49

    Supposed we have:

    class Foo {
        fun bar() = if (someCondition) {
            “Yes”
        } else {
            “No”
        }
    }
    

    There’s nothing preventing us to do the following mocking in the test code:

    val foo = mock()
    whenever(foo.bar()).thenReturn(“Maybe”)
    

    The problem is it is setting up incorrect behavior of class Foo. The real instance of class Foo will never be able to return “Maybe”.

提交回复
热议问题