简体   繁体   English

使用RhinoMocks .NET进行简单模拟的麻烦

[英]Troubles with simple mocking using RhinoMocks .NET

I am trying to experiment with RhinoMocks, where I have to say I am a newbie and probably I don't get some obvious thing here. 我正在尝试使用RhinoMocks,我不得不说我是一个新手,可能我在这里没有明显的东西。 What I'm doing is something like : 我正在做的是:

    [TestMethod]
    public void SaveResponsibleUserFromChangeset()
    {
        var action = mocks.StrictMock<GenomeAction>();
        var changeset = new ActionChangeset();

        changeset.ResponsibleUser = new ChangeableProperty<UserIdentity>("Administrator") {IsChanged = true};
        changeset.MarkAll(true);

        using(mocks.Record())
        {
            Expect.Call(action.ResponsibleUser).SetPropertyAndIgnoreArgument();
        }

        using(mocks.Playback())
        {
            var persistor = new ActionPersistor(new MockIdentityResolver());
            persistor.SaveActionChanges(changeset, action);
        }

        action.VerifyAllExpectations();
    }

    private class MockIdentityResolver : IIdentityResolver
    {
        public GenomeUser GetUser(UserIdentity identity)
        {
            var user = mocks.DynamicMock<GenomeUser>();
            user.Username = identity.Username;
            return user;
        }
    }

The intention is to have a very simple test which checks whether the SaveActionChanges method sets the ResponsibleUser property. 目的是进行一个非常简单的测试,检查SaveActionChanges方法是否设置了ResponsibleUser属性。 As a part of this, it needs to resolve the user identity using the resolver, for which I have provided a mock implementation. 作为其中的一部分,它需要使用解析器来解析用户身份,我为此提供了模拟实现。 Unfortunately, it seems I can't just return back another mock within the Playback mode, because it says (on the closing bracket of the second using) that The action is invalid when the object (of type GenomeUser) is in record state . 不幸的是,似乎我不能在Playback模式中返回另一个模拟,因为它表示(在第二个使用的结束时), 当对象(GenomeUser类型)处于记录状态时该操作无效

Any ideas of what is causing the trouble and how to overcome it ? 是什么导致了麻烦以及如何克服它?

I think you need to create you new MockIdentityResolver() outside the mocks.Playback() . 我认为你需要在new MockIdentityResolver()之外创建new MockIdentityResolver()mocks.Playback()

[TestMethod]
public void SaveResponsibleUserFromChangeset()
{
    var action = mocks.StrictMock<GenomeAction>();
    var changeset = new ActionChangeset();
    var identityResolver;
    changeset.ResponsibleUser = new ChangeableProperty<UserIdentity>("Administrator") {IsChanged = true};
    changeset.MarkAll(true);

    using(mocks.Record())
    {
        Expect.Call(action.ResponsibleUser).SetPropertyAndIgnoreArgument();
        identityResolver = new MockIdentityResolver()
    }

    using(mocks.Playback())
    {
        var persistor = new ActionPersistor(identityResolver);
        persistor.SaveActionChanges(changeset, action);
    }

    action.VerifyAllExpectations();
}

private class MockIdentityResolver : IIdentityResolver
{
    public GenomeUser GetUser(UserIdentity identity)
    {
        var user = mocks.DynamicMock<GenomeUser>();
        user.Username = identity.Username;
        return user;
    }
}

you should look at using the AAA syntax , it seems to be generally accepted that it's a clearer way of using stuff. 你应该看看使用AAA语法 ,似乎普遍认为这是一种更清晰的使用方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM