我有几次这样的事情会有所帮助。例如,我有一个AccountCreator
带有. 我有一个最终将用于创建帐户的帐户。我将首先将属性从to映射到,然后将其传递到 repo 以最终创建它。我的测试看起来像这样:Create
NewAccount
AccountCreator
IRepository
AccountCreator
NewAccount
Account
Account
public class when_creating_an_account
{
static Mock<IRepository> _mockedRepository;
static AccountCreator _accountCreator;
static NewAccount _newAccount;
static Account _result;
static Account _account;
Establish context = () =>
{
_mockedRepository = new Mock<IRepository>();
_accountCreator = new AccountCreator(_mockedRepository.Object);
_newAccount = new NewAccount();
_account = new Account();
_mockedRepository
.Setup(x => x.Create(Moq.It.IsAny<Account>()))
.Returns(_account);
};
Because of = () => _result = _accountCreator.Create(_newAccount);
It should_create_the_account_in_the_repository = () => _result.ShouldEqual(_account);
}
所以,我需要的是替换的东西It.IsAny<Account>
,因为这并不能帮助我验证是否创建了正确的帐户。令人惊奇的是……
public class when_creating_an_account
{
static Mock<IRepository> _mockedRepository;
static AccountCreator _accountCreator;
static NewAccount _newAccount;
static Account _result;
static Account _account;
Establish context = () =>
{
_mockedRepository = new Mock<IRepository>();
_accountCreator = new AccountCreator(_mockedRepository.Object);
_newAccount = new NewAccount
{
//full of populated properties
};
_account = new Account
{
//matching properties to verify correct mapping
};
_mockedRepository
.Setup(x => x.Create(Moq.It.IsLike<Account>(_account)))
.Returns(_account);
};
Because of = () => _result = _accountCreator.Create(_newAccount);
It should_create_the_account_in_the_repository = () => _result.ShouldEqual(_account);
}
请注意,我更改It.IsAny<>
为It.IsLike<>
并传入了一个填充Account
对象。理想情况下,在后台,一些东西会比较属性值,如果它们都匹配则让它通过。
那么,它已经存在了吗?或者这可能是您以前做过的事情并且不介意共享代码?