我正在尝试使用RhinoMocks,我不得不说我是一个新手,可能我在这里没有明显的东西。我正在做的是:
[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;
}
}
目的是进行一个非常简单的测试,检查SaveActionChanges
方法是否设置ResponsibleUser
属性。作为其中的一部分,它需要使用解析器来解析用户身份,我为此提供了模拟实现。不幸的是,似乎我不能在Playback模式中返回另一个模拟,因为它表示(在第二个使用的结束时),当对象(GenomeUser类型)记录时,操作无效状态
关于造成麻烦的原因以及如何克服它的任何想法?
答案 0 :(得分:4)
我认为您需要在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;
}
}
你应该看一下使用AAA syntax,似乎普遍认为这是一种更清晰的使用方式。