As suggested by others, you can overturn your own layout or you can set a series of expectations regarding dependency.
For example, you can verify that your method has been called:
var mock = new Mock<IDependency>(); var subject = new MyClass(mock.Object); subject.Clear(); mock.Verify( dep => dep.Reset(), Times.AtMost(2));
However, it is worth noting that working inside the constructor is a known smell of code , and this smell is exacerbated when trying to write tests.
The fact that your constructor needs to call this method for a dependency suggests that this object knows too much information about the details of the implementation of the dependencies. This violates the principle of open closing and closes you from scripts in which you do not want the Reset method to be called when it is initialized.
Also note that for any class or test that uses a specific MyClass object as a dummy parameter, you will need Mock initialization or you will get a NullReferenceException. This adds significant overhead to writing your tests and adds a level of fragility that equates to long-term maintenance and false negatives in your tests. The only way around this is to make everything an interface, which, although effective, is also not the best long-term strategy.
According to http://googletesting.blogspot.com/2009/07/separation-anxiety.html using Factory will reduce some of this communication and open you up for better reuse of this object.
source share