Rhinomocks DynamicMock 问题

发布于 2024-09-06 19:18:44 字数 1562 浏览 3 评论 0原文

我的动态模拟的行为与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白龙吟 2024-09-13 19:18:44

为什么你要让一个模拟执行你的代码?这就是模拟的意义所在。您模拟了实际的代码,这样您就可以在测试中专注于 1 个且仅 1 个功能区域。

更新

也许我误解了。如果您说它无意中以这种方式运行,则可能是由于使用了具体类而不是接口。

例如替换

var mockProposal = _mockRepository.DynamicMock<BidProposal>();

var mockProposal = _mockRepository.DynamicMock<IBidProposal>();

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

var mockProposal = _mockRepository.DynamicMock<BidProposal>();

with

var mockProposal = _mockRepository.DynamicMock<IBidProposal>();
你げ笑在眉眼 2024-09-13 19:18:44

如果方法声明为虚拟,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.

柠檬心 2024-09-13 19:18:44

当我弄清楚它是什么时,我发布了我最初发布的答案作为问题的更新。方法不是“虚拟”的,因此无法模拟它,因为无法代理非虚拟方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文