RhinoMocks: how to check if a method is called when using PartialMock

I have a class, something like this

public class MyClass
{
  public virtual string MethodA(Command cmd)
  { //some code here}
  public void MethodB(SomeType obj)
  { 
     // do some work
     MethodA(command);
   }

}

I made fun of MyClass as PartialMock ( mocks.PartialMock<MyClass>) and I am setting the wait for MethodA

var cmd = new Command(); 
//set the cmd to expected state
Expect.Call(MyClass.MethodA(cmd)).Repeat.Once();

The problem is that MethodB invokes the actual implementation of MethodA instead of mocking it. I have to do something wrong (not very experienced with RhinoMocks). How to make him make fun of MetdhodA?

Here is the actual code:

  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());
+3
source share
2 answers

I tried to reproduce this problem and it seems to be working fine, what is the difference between my test code (below) and yours?

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();
    }
}
+2
source
0

Source: https://habr.com/ru/post/1744515/


All Articles