Create one common Mock or one per Unit Test

I am currently expanding my Unit Testing using Mock objects (nSubsitute in this particular case). However, I wonder what the current wisdom is in creating Mock objects. For example, I work with an object that contains various procedures for capturing and processing data - there is no biggie here, but it will be used in a large number of tests.

Should I create a generic function that returns a Mock Object with all the appropriate methods and behaviors that mock a large part of the Testing project and call this object in my unit tests? Or I will cheat on an object in each Unit Test, only making fun of the behavior I need for this test (although there will be times when I will mock the same behavior more than once).

Thoughts or advice gratefully received ...

+5
source share
3 answers

I’m not sure if there’s a consistent “current wisdom” on this, but here are my 2 cents.

-, @codebox, mocks unit test , , . , , ( ). , , ([SetUp] NUnit, XUnit), .

mocks , . . , . , .

, , (: ). - When_the_service_is_unavailable, . / (, , ..).

, , Test Data Builder. .

, , , , "". , , , ​​ , .

, . , , . , , , , , .

+9

, .

, : , .

- DRY, .

...

  • , ,

  • , : , , test/mock 1:1,

  • , , . SetUp , ​​, PrepareMock(args...), . , - .

, ( SetUp helper), , , - , .

+1

I would create new layouts for each test - if you reuse them, you may get unexpected behavior when the state of the layout from earlier tests affects the result of subsequent tests.

0
source

All Articles