Rhino Mocks Exception Expect #1 Actual #0:需要帮助

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/369777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:48:12  来源:igfitidea点击:

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

c#exceptionrhino-mocks

提问by Programmin Tool

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

Rhino.Mocks.Exceptions.ExpectationViolationException:
IMockInterface.MockedMethod(); 预期#1,实际#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...

旁注:这是我第一次真正使用 Rhino.Mocks,没有一些内部代码,所以我基本上是边走边学。这里可能有一些非常愚蠢的事情......

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");
}

采纳答案by tvanfosson

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.

您告诉模拟框架在提供程序对象上存根 MockedMethod 类,但您永远不会将提供程序注入要使用的 mainClass 对象。我不清楚您要完成什么,但是如果您希望调用模拟方法,则必须在设置存根的对象上调用它。

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

如果你定义MockThis如下,我想你会发现它会起作用。

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.

最重要的是,您会收到异常,因为该方法从未在提供程序上调用,仅在 mainClass 对象上调用。

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.

通常 ProviderClass 对象会从 ProviderOperation 方法返回 42,但我们已经模拟了它并告诉它返回 -1。调用 ClassUnderTest DoOperation 方法时,会调用模拟提供程序对象的 ProviderOperation 方法并返回模拟值 -1。

Hope this helps.

希望这可以帮助。

回答by Chris

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().

当使用我在测试中构建的对象参数调用存根方法时,我通常会收到此错误,并且在测试代码中,对象是在调用该方法之前构建的。解决方案是使用Rhino.Mocks Matches().

Ex:

前任:

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