visual-studio 无效调用,上次调用已使用或未调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1301116/
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-10-22 10:53:08  来源:igfitidea点击:

Invalid call, the last call has been used or no call has been made

c#visual-studiovisual-studio-2008rhino-mocksrhino-mocks-3.5

提问by Vaccano

I am getting this error when I try to set a mock to have PropertyBehavior():

当我尝试设置模拟时出现此错误PropertyBehavior()

System.InvalidOperationException: System.InvalidOperationException: Invalid call, the last call has been used or no call has been made (make sure that you are calling a virtual (C#) / Overridable (VB) method)..

System.InvalidOperationException: System.InvalidOperationException: Invalid call, 最后一个调用已被使用或没有调用(确保您正在调用虚拟 (C#) / Overridable (VB) 方法)。

I am trying to use only Rhino Mocks 3.5(Arrange, Act, Assert)

我正在尝试仅使用Rhino Mocks 3.5(安排、执行、断言)

Here is my code:

这是我的代码:

    private IAddAddressForm form;

    private AddAddressMediator mediator;

    [TestInitialize()]
    public void MyTestInitialize()
    {
        form = MockRepository.GenerateMock<IAddAddressForm>();
        mediator = new AddAddressMediator(form);


        // Make the properties work like a normal property
        Expect.Call(form.OKButtonEnabled).PropertyBehavior();

        //I tried this too.  I still get the exception
        //SetupResult.For(form.OKButtonEnabled).PropertyBehavior();
    }


    [TestMethod]
    public void TestOKButtonEnabled()
    {

        form.OKButtonEnabled = true;
        Assert.IsTrue(form.OKButtonEnabled);
    }

I know I could use a stub (and for the code above I should) but I am trying to learn Rhino Mocks.

我知道我可以使用存根(对于上面的代码我应该使用),但我正在尝试学习 Rhino Mocks。

Eventually I want to be able to make sure that several properties has their values accessed. (Any hints on how to check that form.FirstNamewas accessed (i.e. the getter was called) would also be appreciated.)

最终,我希望能够确保多个属性的值被访问。(有关如何检查form.FirstName已访问(即调用 getter)的任何提示也将不胜感激。)

In case it is needed, here is the code to IAddressForm:

如果需要,这里是代码IAddressForm

namespace AddressBook
{
    public interface IAddAddressForm
    {
        string FirstName { get; set; }
        string LastName { get; set; }
        string Address1 { get; set; }
        string State { get; set; }
        string Address2 { get; set; }
        string ZipCode { get; set; }
        string City { get; set; }
        bool OKButtonEnabled { get; set; }
    }
}

Anyway, I thought that virtual would not be a problem as I am passing in an interface, but I am clearly missing something.

无论如何,我认为当我传入一个接口时,virtual 不会成为问题,但我显然错过了一些东西。

采纳答案by George Mauer

Never used PropertyBehaviorbefore, but is this the syntax you're looking for?

PropertyBehavior以前从未使用过,但这是您正在寻找的语法吗?

form.Stub(x=>x.OKButtonEnabled).PropertyBehavior()

Rhino Mocks works completely through extension methods now. The only static call I every make any more is to MockRepository.GenerateStub.

Rhino Mocks 现在完全通过扩展方法工作。我每次都发出的唯一静态调用是MockRepository.GenerateStub.

回答by Chris Woodward

You mentioned using a stub instead of a mock but before you go changing it I'd note that strangely, I get the Invalid Call exception when I used GenerateStub but not when I use GenerateMock.

您提到使用存根而不是模拟,但在您更改它之前,我注意到奇怪的是,当我使用 GenerateStub 时我得到了无效调用异常,但在我使用 GenerateMock 时却没有。

View = MockRepository.GenerateStub<IAddressView>();
View.Stub(v => v.Message).PropertyBehavior();

This throws the Invalid call exception and yes, IAddressView.Message does have a getter and setter.

这会引发 Invalid call 异常,是的,IAddressView.Message 确实有 getter 和 setter。

回答by Sean

I received this error when I tried to set an expectation on a non-virtual method.

当我尝试对非虚拟方法设置期望时收到此错误。

mockedObject.Expect(a => a.NonVirtualMethod()).Returns(null);

The error went away when I made NonVirtualMethod virtual.

当我将 NonVirtualMethod 设为虚拟时,错误消失了。

回答by David Ipsen

I ran into this issue when I was trying to call an internalproperty (getter only) on an object in C#. In this case, adding .PropertyBehavior()did not help.

当我尝试internal在 C# 中的对象上调用属性(仅限 getter)时遇到了这个问题。在这种情况下,添加.PropertyBehavior()没有帮助。

My solution was to extract the logic out of the property and into an internal method which I then injected dependencies into this method (as parameters).

我的解决方案是将逻辑从属性中提取到一个内部方法中,然后我将依赖项注入到这个方法中(作为参数)。

回答by Grzenio

I think you have to do MockRepository.ReplyAll() after you set up all expectations and before you start using this mock. So my guess in your case is that you have to move the Expect.Call line before mediator = new AddAddressMediator(form);, and stick the reply all right after that:

我认为您必须在设置所有期望之后和开始使用此模拟之前执行 MockRepository.ReplyAll() 。所以我的猜测是,你必须在 mediator = new AddAddressMediator(form); 之前移动 Expect.Call 行,并在此之后立即回复:

[TestInitialize()]
public void MyTestInitialize()
{
    form = MockRepository.GenerateMock<IAddAddressForm>();
    // Make the properties work like a normal property
    Expect.Call(form.OKButtonEnabled).PropertyBehavior();

    //I tried this too.  I still get the exception
    //SetupResult.For(form.OKButtonEnabled).PropertyBehavior();

    MockRepository.ReplyAll();
    mediator = new AddAddressMediator(form);



}