犀牛Mo例外期望#1实际#0:需要帮助 [英] Rhino Mocks Exception Expect #1 Actual #0 : Need assistance

查看:58
本文介绍了犀牛Mo例外期望#1实际#0:需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经对此进行了搜索,但似乎所有内容都可以使用,但是不幸的是,我所阅读的所有内容都无法帮助您找到答案.这是课程:

 公共接口IMockInterface{MockClass MockedMethod();MockClass MockThis();}公共类MockClass:IMockInterface{公共虚拟MockClass MockedMethod(){MockClass returnValue;returnValue = new MockClass();returnValue.SomeMessage =未嘲笑";return returnValue;}公共MockClass MockThis(){MockClass模拟;MockClass returnValue;模拟=新的MockClass();返回嘲笑.MockedMethod();}} 

测试:

  public void MockTest_Internal(){MockClass mainClass;MockClass returnClass;IMockInterface模拟提供程序;mainClass =新的MockClass();模拟提供者=存储库.StrictMock< IMockInterface>();Expect.Call(mockProvider.MockedMethod()).Return(new MockClass {SomeMessage ="Mocked"});repository.ReplayAll();returnClass = mainClass.MockThis();provider.AssertWasCalled(item => item.MockedMethod());Assert.IsTrue(returnedClass.SomeMessage ==模拟");} 

并且也尝试过并且不起作用

但我不断收到此异常:

Rhino.Mocks.Exceptions.ExpectationViolationException:
IMockInterface.MockedMethod();预期#1,实际#0

现在,根据我所读的内容,这可能表明该方法是使用与预期参数不同的方法调用的,或者该方法从未被调用但应被调用.测试不是这种情况.

侧面说明:这是我第一次真正使用Rhino.Mocks,没有内部代码,所以我基本上是随手拿起它.这里可能有些愚蠢的事情……

这是对旧测试的评论,但不是我应该使用的内容:

  public void MockTest_Internal(){MockClass mainClass;MockClass returnClass;IMockInterface模拟提供程序;mainClass =新的MockClass();var provider = MockRepository.GenerateStub< IMockInterface>();provider.Stub(item => item.MockedMethod()).Return(new MockClass {SomeMessage ="Mocked"});returnClass = mainClass.MockThis();provider.AssertWasCalled(item => item.MockedMethod());Assert.IsTrue(returnedClass.SomeMessage ==模拟");} 

解决方案

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

如果您按以下方式定义 MockThis ,我想您会发现它会起作用.

 公共MockClass MockThis(IMockInterface提供程序){返回provider.MockMethod();} 

最重要的是,您将获得异常,因为从未在提供程序上调用过该方法,而仅在mainClass对象上调用了该方法.

编辑:示例

 公共类ClassUnderTest{私有ProviderClass提供者{放;}公共ClassUnderTest(ProviderClass provider){this.Provider =提供程序;}public int DoOperation(){返回this.Provider.ProviderOperation();}}公共类ProviderClass{private int值= 42;公共ProviderClass(){}公共虚拟int ProviderOperation(){返回this.value;}}[测试方法]公共无效DoOperationTest(){ProviderClass模拟提供者= MockRepository.GenerateMock< ProviderClass>();mockProvider.Expect(mp => mp.ProviderOperation()).Return(-1);ClassUnderTest目标=新的ClassUnderTest(mockProvider);int ExpectedValue = -1;int值= target.DoOperation();Assert.AreEqual(ExpectedValue,value);mockProvider.VerifyAllExpectations();} 

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

希望这会有所帮助.

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

解决方案

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.

这篇关于犀牛Mo例外期望#1实际#0:需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆