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