Rhinomocks DynamicMock question
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(开发者_StackOverflow
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.
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>();
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.
精彩评论