0

I'm working on understanding a bit more about Setup and unit testing with Moq. I've run into a slight problem though.

What I want to do is something like this:

view.Setup(x => x.GetReference("object1")).Returns(object1);
view.Setup(x => x.GetReference("object2")).Returns(null);

However, when I make my call this way, I never hit the block of code that would react to the Null statement. How am I supposed to set up my Setups so that they will behave in a specific way when they are called by a specific argument?

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Chip Snyder
  • 418
  • 3
  • 13
  • 1
    You'll probably need to provide more code around the test setup to give an idea of what you're trying. The mock is setup expecting the test to call view.GetReference("object2") which if you're not getting your expected null return, then the mock isn't getting called with an "object2" parameter. Can you post the complete unit test and possibly the code under test that would be triggering the GetReference call? – Steve Py Jul 30 '12 at 06:06

1 Answers1

0

The moq overloads two ways to return a value:

  1. instance: Returns(instance);
  2. delegate(Func<T>): Returns(()=>new Foo());

I think that the problem is caused from the ambiguousness for which Returns method is to be used.

So, you need to pass in the explicit type of NULL for the second setup of your code as the following ways:

  1. view.Setup(x => x.GetReference("object2")).Returns((ExplicitType)null);
  2. view.Setup(x => x.GetReference("object2")).Returns(() => null);
Jin-Wook Chung
  • 4,196
  • 1
  • 26
  • 45