KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I would like to unit test a method on a class I have made, but this method requires another method to be called first. Example: // This would work MyClass myClass1 = new MyClass(mockDevice); myClass1.Run(myDatastructure); myClass1.Stop(); // This would throw an InvalidOperationException MyClass myClass2 = new MyClass(mockDevice); myClass2.Stop(); Run is starting an operation on a hardware device, and Stop is of course trying to stop that operation (sending a reset-command and starting a timeout-timer). Anyway I would like to test various post-conditions of calling Stop , but I would like NOT to have to call Run , because I am testing Stop - not Run ! I would like something like this: MyClass myClass = new MyClass(mockDevice); myClass.Stop(); Assert.IsTrue(mockDevice.ResetCalled); So far I only see one possible solution, and that is to create a TestableMyClass that inherits from MyClass , that makes it possible to set the right internal state of the MyClass instance before calling Stop . The problem with this solution is that I have to change my MyClass -implementation to have protected members instead of private members, and I don't like the idea of having to change the implementation in order to test it! Should I use this solution, is there an error in my design, or is there a smarter way of doing this?
Tags (comma-separated)
Save Edits
Cancel