I\'ve just started writing unit tests and I\'m not sure am I doing it the \"righ
ID: 644196 • Letter: I
Question
I've just started writing unit tests and I'm not sure am I doing it the "right way." Should I test every class "by removing dependencies"? In other words, does unit testing mean: "test part of a code with assumption that provided dependencies to other modules are correct and test these dependencies as single modules later in other unit test classes"?
For example, I write WinRT application in Model-View-ViewModel manner and I have MainViewModel class. This class has dependency to IDataRepository which is injected in constructor. MainViewModel is responsible for handling commands and connecting model with view. MainViewModel has commands like CopyItems, LoadData etc which are relying on IDataRepository implementation.
Should I pass fake (mock/fake it??) IDataRepository in MainViewModel_Tests and test just is data loaded, copied etc. on command execution? Should one test check just things what class really do - not what dependencies do?
Explanation / Answer
Should I pass fake (mock/fake it??) IDataRepository in MainViewModel_Tests and test just is data loaded, copied etc. on command execution?
Yes. As far as possible unit tests should test a single class. Any dependency that connects to any data source outside the test process should be mocked. This includes local system state like the system clock.
Should one test check just things what class really do - not what dependencies do?
Yes, otherwise your tests will become very fragile. A change in the behavior of a dependent class may require changes to all tests that depend on that class. Ideally each behavior is tested exactly once.
Of course you will still need to do integration testing.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.