Can we use more than one mock object in a unit test?
You should add as many mockups as needed to isolate the class under test. You need a mock for each dependency, which shouldn't be part of the test.
Sometimes you put two or three classes together in a test for simplicity, because they create something like a component and are tightly coupled. Everything else should be mocked.
I know this "best practice" to only have one layout and also don't understand. We have a lot of mocks in our unit tests, some environmental layouts are set up by a test framework I wrote (e.g. TransactionService, SecurityService, SessionService). There is only one thing to consider, as Guiche already mentioned in his answer, a lot of bullying is indicative of high addiction. It's up to you when it's too much. We have a lot of small interfaces that require a lot of mockups in tests.
To enable the answer, you shouldn't mock a dependency if:
- This is a very related part of the class under test, like inner class, private class, etc.
- It is a common .NET framework class like Collection etc.
- You want to write an integration test to accurately test interactions with this class. (You are still mocking everything else, and you still have unit tests for each involved class in isolation.)
- It's just expensive to mock a certain class. Be careful, considering this too expensive, mock seems to be difficult to set up, but it seems to be a breeze compared to the maintainability problems you would have using real classes. But there are some frameworks and technologies that are not implemented against interfaces, and they are very difficult to mock. If it's too expensive to put these framework classes behind their own interface, you need to live with them in tests.
a source to share
There may be several falsifications in the unit test depending on the context.
However, I think the "articles" may hint that
- preventing over-ridicule . When a single test makes fun of all the collaborators, you leave the door open; the script may fail if you replace the actual contributors. By minimizing the number of layouts and using real collaborators as much as possible / possible, you minimize this risk.
- High connectivity alerts . If you have to mock a lot of employees to write a unit test, it might be a designer scent indicating that you have a strong connection.
a source to share