views:

265

answers:

4

I am writing unit for a class which looks like following using nunit and Rhino mock.

Class MyClass
{
  private void M()
  {
    N("Hi");
  }

  private void N(string text)
  {
    ........ do something
  }
}

For the unit test for method M I want to check if method N was called with argument "Hi". How do I do it?

+7  A: 

It seems to me that from the point of view of testing, you're delving into the implementation details of your object. Can't you perform your tests by checking the ultimate result of your method call ? That is, presumably these method calls have some effect. So rather than checking the arguments being passed, you should be checking the final result.

That way you can change your underlying code at a later date, and your unit tests will confirm that the ultimate result is the same, independent of your implementation.

Brian Agnew
Brian I totally agree with you but in my class method N is called at several places so what I have done is I have return unit test for method N. So do not intend to check the result of execution of method N in unit test of method M. Besides in my case method N contribute very little towards the result of method M.
Prithis
could be you are trying to do too much in one class. are you sure method shouldn't be refactored out to another class
jk
If it's used in several places refactor the code out. Make the methods publicily visible and test. Then you can use that class within your code but still have the implementation details hidden.
Finglas
+1  A: 

Use mocking on your method N().

http://www.mockobjects.com/

However, Brian solution is better -- think it's a good direction for good unit testing.

Mihail
A: 

+1 to Brian's response.

The alternative is to split "N" out to a different class, and then use a mocked instance of that class in your test. You can then set up the mock to expect a call with a specific parameter. But, it might not actually be appropriate to split it out. Depends on your exact scenario.

AdaTheDev
Nice idea but as you mentioned it is not worth doting in my scenario.
Prithis
A: 

Following code can help you.

var mock = new Mock(); bool called=false; string test=string.empty; mock.Setup(foo => foo.Execute(It.IsAny())).Callback((string s) => { test = s; called = true;}); Assert.IsTrue(called, "Execute() was not called");

pradeep tiwari