gpt4 book ai didi

c# - 犀牛模拟 : how to test if method was called when using PartialMock

转载 作者:行者123 更新时间:2023-11-30 17:22:18 26 4
gpt4 key购买 nike

我有一个类似这样的类

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());

最佳答案

我已经尝试重现这个问题,它似乎工作正常,我的测试代码(如下)和你的有什么不同?

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();
}
}

关于c# - 犀牛模拟 : how to test if method was called when using PartialMock,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2789117/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com