Search code examples
c#unit-testingmockingrhino-mocksrhino

How to mock a not implemented method with Rhino Mock?


I have this simplified implementation and the unit test below:

public class Parent
{
    public virtual int GetSomeValue()
    {
        throw new NotImplementedException();
    }
}

public class Child
{
    public Parent MyParent { get; set; }

    public virtual Parent GetParent()
    {
        return MyParent;
    }

    public virtual int GetParentsValue()
    {
        var parent = GetParent();

        return parent.GetSomeValue();
    }
}

How can I test the GetParentsValue() method with Rhino Mock without implement the parent's GetSomeValue() method?

Thanks!


Solution

  • You can do this:

    Child target = new Child();
    
    Parent mockParent = MockRepository.GenerateStub<Parent>();
    mockParent.Stub(x => x.GetSomeValue()).Return(1);
    
    target.MyParent = mockParent;
    
    int value = target.GetParentsValue();
    
    Assert.AreEqual(value, 1);