Reputation: 671
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!
Upvotes: 0
Views: 605
Reputation: 67296
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);
Upvotes: 2
Reputation: 39898
You can use this code:
Child child = MockRepository.GenerateStrictMock<Child>();
child.Stub(c => c.GetParentsValue()).Return(1);
Assert.AreEqual(1, child.GetParentsValue());
If you want to test some internals of the GetParentsValue() method you should mock Parent.GetSomeValue()
with:
Parent mockParent = MockRepository.GenerateStub<Parent>();
mockParent.Stub(x => x.GetSomeValue()).Return(1);
target.MyParent = mockParent;
Upvotes: 1