When to use mock objects in unit tests

后端 未结 5 896
北恋
北恋 2020-12-04 02:27

I\'m aware that there are many questions about mocking and testing, but I didn\'t find any that helped me perfectly, so I still have problems understanding the follwing:

5条回答
  •  温柔的废话
    2020-12-04 02:50

    In your particular example, no, you would not need to mock anything.

    Let's focus on what you would test:

    1. a test where you add and retrieve one Citizen
    2. add 2 Citizens, retrieve one
    3. pass null instead of citizen, and make sure your code doesn't break.
    4. add two citizens with the same name, what would you expect to happen then?
    5. add a citizen without a name.
    6. add a citizen with a null name

    etc etc

    You can already see a number of different tests you can write.

    To make it more interesting you could add some code to your class which exposes a read-only version of your citizenList, then you could check that your list contains exactly the right things.

    So, in your scenario you don't need to mock anything as you don't have external dependencies on another system of some kind. Citizen seems to be a simple model class, nothing more.

提交回复
热议问题