Rhino Mock - Stubbed method always returns null

1.2k Views Asked by At

I've been trying to figure out what is happening with this particular test that I'm writing and now reaching out again to see if someone might know what is going on.

I have a ModeltoXml method that when I test via another test class directly, it's fine. However in another method I'm testing which makes a call to ModeltoXml my stub created in Rhino Mock always returns null.

Below is roughly how my code looks for the test where I pass my stubbed methods in via the constructor.

[Test]

   var newEntry = new Model1{name="test"};
       var referrer = "http://example.com";

       var stubbedConfigMan = MockRepository.GenerateStub<IConfigurationManager>();
       var supportMethods = MockRepository.GenerateStub<ISupportMethods>();
       stubbedConfigMan.Stub(x => x.GetAppSetting("confEntry1")).Return("pathhere");
       supportMethods.Stub(x => x.ModeltoXml(newEntry)).IgnoreArguments().Return("test"); //this is the line of code which should be setting the return value

 var testObject = new MyApi().NewTicket(newEntry, referrer, supportMethods, stubbedConfigMan);

The concrete method that is invoked for this test has a line which looks like this:

 public MyResponseModel NewTicket(Model1 newTicket, string referrer,     ISupportMethods supportMethods,IConfigurationManager configMan)
{
//.. other code here
   var getXml = supportMethods.ModeltoXml(newTicket);  //This line always returns null
}

A simple entry for the ModeltoXml method:

public string ModeltoXml<T>(T item)
{
//Code here to serialize response

       return textWriter.ToString();
   }

And what I have on my interface:

public interface ISupportMethods
{
  string ModeltoXml<T>(T item);
}

I created another basic method which would except and return a string value. I got this working in the same class however in the test I had to use the .IgnoreArguments option on the stub. Thereafter when debugging I was seeing the set return value.

However when I tried doing the same on the problematic line of code in my test class, I still see no return value appearing in my class under test during debug.

So I'm a tad stuck! I'm getting return values on other stubs in my test and it's this particular method/stub which just wont play ball.

Thanks.

UPDATE

A more simple example of the above I've found is under the same test:

       var stubbedConfigMan = MockRepository.GenerateStub<IConfigurationManager>();
       var supportMethods = MockRepository.GenerateStub<ISupportMethods>();

        supportMethods.Stub(x => x.ModeltoXml(newEntry)).IgnoreArguments().Return("test").Repeat.Any();

//the class under test is successfully returning 'hero' when this method is called.
       supportMethods.Stub(x => x.mytest(Arg<string>.Is.Anything)).Return("hero");

//If I use this line and debug test9 is set with my return value of "test".
//However in the same run my class under test the method just returns null.
    var test9 = supportMethods.ModeltoXml(newEntry);

   var testObject = new MyApi().NewTicket(newEntry, referrer, supportMethods, stubbedConfigMan);

What the above examples shows is that passing my mocked/stub objects over to my class under test, something happens with the ModeltoXml stub. I can't see/think of any reason why this is happening.

1

There are 1 best solutions below

0
Sulphy On

At last got this one sorted. I had a feeling the problem was related to my use of generics in my ModeltoXml method. I have simply used the Arg property in my ModeltoXml stub which has solved the problem:

  supportMethods.Expect(x => x.ModeltoXml(Arg<Model1>.Is.Anything)).Return("Model XML response");

I also used .Expect this time around so that I could assert the method was called but it will work with .Stub all the same.