views:

73

answers:

1

I'm having a bit of trouble doing some unit-testing using moq.

If I have a function like this:

public string GetName(IMapinfoWrapper wrapper)
{
   return wrapper.Evaluate("My com command");
   ///"My comm command" is the same all the time.
}

Then I have a test that checks the return value of the GetName function:

[Test]
public void Test()
{
  Mock<IMapinfoWrapper> mock = new Mock<IMapinfoWrapper>();
  mock.Expect(mapinfo => mapinfo.Evaluate(It.IsAny<String>()))
      .Returns("TableName");

  Assert.AreEqual("TableName", GetName(mock.object));
}

So my question is should the mock be define like this:

mock.Expect(mapinfo => mapinfo.Evaluate(It.IsAny<String>()))
    .Returns("TableName");

or like this:

mock.Expect(mapinfo => mapinfo.Evaluate("My com command")
    .Returns("TableName");

My only problem with using the second way is that I feel like I'm tying the implementation of the method GetName to always use the "My com command" string internally.

Is this what I should be doing or am I doing this all wrong.?
I have only started doing unit-testing and mocking so I'm still not sure how everything should go together.

+3  A: 

It depends on what you are actually testing.

If the expectation is that any string passed to Evaluate will return "TableName", then use the call to IsAny.

Otherwise, if you are expecting specific output given specific input, like the case of passing "My com command", then pass the second.

It seems like you aren't looking for specific input, but rather (in this case) doing basic sanity checking, so I would say the former is the right move (again in this case, but you should have more specific cases if you have the use cases for it).

casperOne