Rhino Mocks Exception Expect #1 Actual #0 : Need assistance

南笙酒味 提交于 2021-01-27 06:58:47

问题


I've have searched on this and it seems to be a catch all, unfortunately everything I've read doesn't help figure it out. Here is the class:

public interface IMockInterface
{
    MockClass MockedMethod();
    MockClass MockThis();
}

public class MockClass : IMockInterface
{
  public virtual MockClass MockedMethod()
  {
    MockClass returnValue;

    returnValue = new MockClass();
    returnValue.SomeMessage = "Not mocked";
    return returnValue;
  }

  public MockClass MockThis()
  {
    MockClass mock;
    MockClass returnValue;

    mock = new MockClass();

    return mock.MockedMethod();
  }
}

And the test:

public void MockTest_Internal()
{
  MockClass mainClass;
  MockClass returnedClass;
  IMockInterface mockProvider;

  mainClass = new MockClass();

  mockProvider = repository.StrictMock<IMockInterface>();
  Expect.Call(mockProvider.MockedMethod())
    .Return(new MockClass { SomeMessage = "Mocked" });
  repository.ReplayAll();

  returnedClass = mainClass.MockThis();
  provider.AssertWasCalled(item => item.MockedMethod());

  Assert.IsTrue(returnedClass.SomeMessage == "Mocked");
}

And have also tried and doesn't work

But I keep getting this exception:

Rhino.Mocks.Exceptions.ExpectationViolationException:
IMockInterface.MockedMethod(); Expected #1, Actual #0

Now from what I've read this would suggest either the method was called with different than expected parameters OR the method was never called but was expected to be called. This isn't the case for the test.

Side Note: This is my first time really using Rhino.Mocks without some in house code so I am basically picking it up as I go. There could be something really stupid here...

This was the old test commented on, but is not what I should have been using:

public void MockTest_Internal()
{
  MockClass mainClass;
  MockClass returnedClass;
  IMockInterface mockProvider;

  mainClass = new MockClass();

  var provider = MockRepository.GenerateStub<IMockInterface>();
  provider.Stub(item => item.MockedMethod())
    .Return(new MockClass { SomeMessage = "Mocked" });

  returnedClass = mainClass.MockThis();
  provider.AssertWasCalled(item => item.MockedMethod());

  Assert.IsTrue(returnedClass.SomeMessage == "Mocked");
}

回答1:


You're telling the mock framework to stub the MockedMethod class on the provider object, but you never inject the provider into the mainClass object to be used. It's not clear to me what you are trying to accomplish but if you want the mocked method to be called then it has to be called on the object on which the stub was set up.

If you define MockThis as below, I think you will find that it will work.

public MockClass MockThis(IMockInterface provider)
{
    return provider.MockMethod();
}

The bottom line is that you get the exception because the method was never called on the provider, only on the mainClass object.

EDIT: Example

public class ClassUnderTest
{
    private ProviderClass provider { get; set; }

    public ClassUnderTest( ProviderClass provider )
    {
        this.Provider = provider;
    }

    public int DoOperation()
    {
        return this.Provider.ProviderOperation();
    }
}

public class ProviderClass
{
    private int value = 42;
    public ProviderClass()
    {
    }

    public virtual int ProviderOperation()
    {
        return this.value;
    }
}


[TestMethod]
public void DoOperationTest()
{
     ProviderClass mockProvider = MockRepository.GenerateMock<ProviderClass>();
     mockProvider.Expect( mp => mp.ProviderOperation() ).Return( -1 );

     ClassUnderTest target = new ClassUnderTest( mockProvider );

     int expectedValue = -1;
     int value = target.DoOperation();

     Assert.AreEqual( expectedValue, value );

     mockProvider.VerifyAllExpectations();
}

Normally the ProviderClass object would return 42 from the ProviderOperation method, but we've mocked it out and told it to return -1. When the ClassUnderTest DoOperation method is called, the mock provider object's ProviderOperation method is invoked and returns the mocked value of -1.

Hope this helps.




回答2:


I usually get this error when a stubbed method is called with an object argument that I build in the test and in the tested code the object is built before calling that method. The solution is to use the Rhino.Mocks Matches().

Ex:

Arg<string>.Matches(s => s.Contains("some substring"))


来源:https://stackoverflow.com/questions/369777/rhino-mocks-exception-expect-1-actual-0-need-assistance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!