Rhinomocks DynamicMock 问题
我的动态模拟的行为与 Parial 模拟相同,这意味着它在调用时执行实际的代码。以下是我尝试的方法,它
var mockProposal = _mockRepository.DynamicMock<BidProposal>();
SetupResult.For(mockProposal.CreateMarketingPlan(null, null, null)).IgnoreArguments().Repeat.Once().Return(
copyPlan);
//Expect.Call(mockProposal.CreateMarketingPlan(null, null, null)).IgnoreArguments().Repeat.Once().Return(
// copyPlan);
// mockProposal.Expect(x => x.CreateMarketingPlan(null, null, null)).IgnoreArguments().Return(copyPlan).Repeat.Once();
不只是返回我期望的内容,而是在方法 CreateMarketingPlan 中运行代码,
这是错误:
System.NullReferenceException: Object reference not set to an instance of an object.
at Policy.Entities.MarketingPlan.SetMarketingPlanName(MarketingPlanDescription description) in MarketingPlan.cs: line 76
at Policy.Entities.MarketingPlan.set_MarketingPlanDescription(MarketingPlanDescription value) in MarketingPlan.cs: line 91
at Policy.Entities.MarketingPlan.Create(PPOBenefits ppoBenefits, MarketingPlanDescription marketingPlanDescription, MarketingPlanType marketingPlanType) in MarketingPlan.cs: line 23
at Policy.Entities.BidProposal.CreateMarketingPlan(PPOBenefits ppoBenefits, MarketingPlanDescription marketingPlanDescription, MarketingPlanType marketingPlanType) in BidProposal.cs: line 449
at Tests.Policy.Services.MarketingPlanCopyServiceTests.can_copy_MarketingPlan_with_all_options() in MarketingPlanCopyServiceTests.cs: line 32
更新:我弄清楚它是什么。方法不是“虚拟”的,因此无法模拟它,因为无法代理非虚拟方法。
My dynamic mock behaves as Parial mock, meaning it executes the actual code when called. Here are the ways I tried it
var mockProposal = _mockRepository.DynamicMock<BidProposal>();
SetupResult.For(mockProposal.CreateMarketingPlan(null, null, null)).IgnoreArguments().Repeat.Once().Return(
copyPlan);
//Expect.Call(mockProposal.CreateMarketingPlan(null, null, null)).IgnoreArguments().Repeat.Once().Return(
// copyPlan);
// mockProposal.Expect(x => x.CreateMarketingPlan(null, null, null)).IgnoreArguments().Return(copyPlan).Repeat.Once();
Instead of just returning what I expect it runs the code in the method CreateMarketingPlan
Here is the error:
System.NullReferenceException: Object reference not set to an instance of an object.
at Policy.Entities.MarketingPlan.SetMarketingPlanName(MarketingPlanDescription description) in MarketingPlan.cs: line 76
at Policy.Entities.MarketingPlan.set_MarketingPlanDescription(MarketingPlanDescription value) in MarketingPlan.cs: line 91
at Policy.Entities.MarketingPlan.Create(PPOBenefits ppoBenefits, MarketingPlanDescription marketingPlanDescription, MarketingPlanType marketingPlanType) in MarketingPlan.cs: line 23
at Policy.Entities.BidProposal.CreateMarketingPlan(PPOBenefits ppoBenefits, MarketingPlanDescription marketingPlanDescription, MarketingPlanType marketingPlanType) in BidProposal.cs: line 449
at Tests.Policy.Services.MarketingPlanCopyServiceTests.can_copy_MarketingPlan_with_all_options() in MarketingPlanCopyServiceTests.cs: line 32
Update: I figured out what it was. Method was not "virtual" so it could not be mocked because non-virtual methods cannot be proxied.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么你要让一个模拟执行你的代码?这就是模拟的意义所在。您模拟了实际的代码,这样您就可以在测试中专注于 1 个且仅 1 个功能区域。
更新:
也许我误解了。如果您说它无意中以这种方式运行,则可能是由于使用了具体类而不是接口。
例如替换
为
Why would you have a mock executing your code?? That's the point of a mock. You mock out your actual code so you can focus on 1 and only 1 area of functionality in a test.
Update:
Maybe I misunderstood. If you are saying it's behaving in this way unintentionally, it's possibly due to using a concrete class instead of an interface.
e.g. Replace
with
如果方法声明为虚拟,Rhino 支持在具体类上模拟方法。所以你可以通过在你想要记录的方法的每个方法声明中添加 virtual 关键字来解决这个问题。您还可以按照 Andy_Vulhop 的建议提取接口。
Rhino supports mocking methods on concrete classes if the methods are declared virtual. So you can solve this problem by adding the virtual keyword to each method declaration for methods you want to record. You could also extract an Interface as Andy_Vulhop suggested.
当我弄清楚它是什么时,我发布了我最初发布的答案作为问题的更新。方法不是“虚拟”的,因此无法模拟它,因为无法代理非虚拟方法。
As I figured out what it was I am posting the answer that I originally posted as update to the question. Method was not "virtual" so it could not be mocked because non-virtual methods cannot be proxied.