tags:

views:

18

answers:

1

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.

A: 

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>();
Andy_Vulhop
Why? Because I don't care what happens in that method, I just want to assert that it is called once. I am not testing that method, I am testing some other method, that calls CreateMarketingPlan on mocked BidProposal.
epitka
The way I read it, I thought you meant that you had the mock calling into your BidProposal code, which seemed highly pointless. Like I said in the update, if you want to mock your BidProposal, you might want an interface for RhinoMocks to mock out. If BidProposal doesn't implement an IBidProposal interface, make one, then use that interface (as per the Update in my answer).
Andy_Vulhop