Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 6 down vote accepted

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.

share|improve this answer
    
Thanks. Can integration testing be done with same unit test framework or there are other tools for that? –  fex Dec 21 '13 at 22:45
    
For code-driven testing the same frameworks can be used. There are separate frameworks for UI testing. –  kevin cline Dec 22 '13 at 0:27
2  
Just like to add that mocking isn't faking. Mocked objects should be used to assist in testing a class. Use mock objects to simulate difficult to test scenarios. Like disk IO errors or lost database connections. –  Mathew Foscarini Dec 22 '13 at 1:31
    
Just to add an extra note about the "This includes local system state like the system clock." I would say that anything that is an external dependency of the class that is not related to the class's functionality gets this treatment. So for example, say I have a class that reads/writes a configuration file but (for some strange reason) also takes in a time source. I would pass in the time source as an interface and mock or stub it out, but the dependency on the file system is central to the purpose of the class, so that would be left as-is. At some point you have to touch real devices. –  J Trana Dec 22 '13 at 4:55
    
@JTrana: in most modern languages IO is done via streams rather than physical files. A configuration file reader could easily be tested using data inline to the test. –  kevin cline Dec 23 '13 at 17:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.