私はこのような何かが役立つだろういくつかの機会がありました。たとえば、をとるメソッドを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
それで、それはすでに存在しますか?または、これは以前に行ったことがあるので、コードを共有してもかまいませんか?