我有类似的东西
public class MyClass
{
public virtual string MethodA(Command cmd)
{ //some code here}
public void MethodB(SomeType obj)
{
// do some work
MethodA(command);
}
}
我将MyClass模拟为PartialMock(mocks.PartialMock<MyClass>
)并设置了MethodA的预期
var cmd = new Command();
//set the cmd to expected state
Expect.Call(MyClass.MethodA(cmd)).Repeat.Once();
问题是MethodB调用MethodA的实际实现而不是模拟它。我必须做错事(对RhinoMocks不是很有经验)。我如何强迫它模仿MetdhodA?
以下是实际代码:
var cmd = new SetBaseProductCoInsuranceCommand();
cmd.BaseProduct = planBaseProduct;
var insuredType = mocks.DynamicMock<InsuredType>();
Expect.Call(insuredType.Code).Return(InsuredTypeCode.AllInsureds);
cmd.Values.Add(new SetBaseProductCoInsuranceCommand.CoInsuranceValues()
{
CoInsurancePercent = 0,
InsuredType = insuredType,
PolicySupplierType = ppProvider
});
Expect.Call(() => service.SetCoInsurancePercentages(cmd)).Repeat.Once();
mocks.ReplayAll();
//act
service.DefaultCoInsurancesFor(planBaseProduct);
//assert
service.AssertWasCalled(x => x.SetCoInsurancePercentages(cmd),x=>x.Repeat.Once());
答案 0 :(得分:2)
我试图重现这个问题,它似乎工作正常,我的测试代码(下面)和你的测试代码有什么不同?
public class MyClass
{
public virtual string MethodA(object cmd)
{
return "implementation";
}
public string MethodB(object obj)
{
// do some work
return MethodA(obj);
}
}
[TestFixture]
public class MyClassTests
{
[Test]
public void MockTest()
{
var myClassMock = MockRepository.GenerateMock<MyClass>();
myClassMock.Expect(x => x.MethodA("x")).Return("mock");
Assert.AreEqual("mock", myClassMock.MethodB("x"));
myClassMock.VerifyAllExpectations();
}
}
答案 1 :(得分:0)
此处的问题似乎是,在您的评论中,您说PartialMock,但在您的代码示例中,您使用了DynamicMock。我相信这是你问题的根源,因为它们的行为不同。