У меня было несколько случаев, когда что-то вроде этого было бы полезно. Я имею, например, AccountCreator
с 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
. В идеале, в фоновом режиме, что-то сравнивало бы значения свойств и пропускало бы их, если они все совпадают.
Итак, он уже существует? Или это может быть что-то, что вы делали раньше и не хотели бы делиться кодом?