mardi 2 juin 2015

RhinoMock - Use a real object, but stub a single method

I'm writing a unit test against an MVC Controller that has a dependency on IFoo. Foo (the implementation) has one method I'd like to stub, but leave the other in tact. How can I set this up using RhinoMock?

Foo has several dependencies that I'd prefer not to mock to save writing additional lines of code and cluttering my test.

Foo :

public interface IFoo{
    int Method1();
    int Method2();
}

public class Foo : IFoo{
   //lot's of dependencies
   public Foo(IBar bar, IBaz baz, IStackOverflow so){}
}

Test:

[Test]
public void What_I_Have_So_Far(){
    //arrange
    //load the real IFoo from Ninject (DI)
    var mockFoo = new Ninject.Kernel(new MyExampleModule())
                    .Get<IFoo>();

    //I want this test to use the real Method1, but not Method2
    //so stub Method2
    mockFoo
       .Stub(x => x.Method2()) //<---- blows up here
       .Returns(42);

    //act
    var controllerUnderTest = new Controller(mockFoo);

Error:

Using this approach, RhinoMock throws an Exception:

System.InvalidOperationException : The object 'SKWebApi.Services.TechDataPayloadParser' is not a mocked object.

Question:

How can I stub method2?

I know I could create IFoo as a mock via MockRepository.GenerateMock, but then'd I'd have to copy the real implementation of Method1.

Aucun commentaire:

Enregistrer un commentaire