39

私はこのような何かが役立つだろういくつかの機会がありました。たとえば、をとるメソッドをAccountCreator持つがあります。私は、最終的にアカウントを作成するために使用されるを持っています。最初にプロパティをからにマップし、次にをリポジトリに渡して最終的に作成します。私のテストは次のようになります。CreateNewAccountAccountCreatorIRepositoryAccountCreatorNewAccountAccountAccount

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

それで、それはすでに存在しますか?または、これは以前に行ったことがあるので、コードを共有してもかまいませんか?

4

4 に答える 4

46

同様の基準に基づいて特定の値を返すようにリポジトリをスタブアウトするには、次のように機能する必要があります。

_repositoryStub
    .Setup(x => x.Create(
        Moq.It.Is<Account>(a => _maskAccount.ToExpectedObject().Equals(a))))
    .Returns(_account);
于 2012-07-05T19:43:58.953 に答える
19

以下はあなたのために働くはずです:

Moq.It.Is<Account>(a=>a.Property1 == _account.Property1)

ただし、前述のとおり、一致基準を実装する必要があります。

于 2012-07-03T05:32:09.277 に答える
0

質問に記載されていることを正確に実行するものを見つけることができませんでした。それまでの間、モックされたメソッドに引数として渡されたオブジェクトの検証を処理するために私が見つけることができる最善の方法はCallback、実際のオブジェクトと期待されるオブジェクトを比較するための期待されるオブジェクトパターンとの組み合わせです。

public class when_creating_an_account
{
    static Mock<IRepository> _mockedRepository;
    static AccountCreator _accountCreator;
    static NewAccount _newAccount;
    static Account _result;
    static Account _expectedAccount;
    static Account _actualAccount;

    Establish context = () =>
        {
            _mockedRepository = new Mock<IRepository>();
            _accountCreator = new AccountCreator(_mockedRepository.Object);

            _newAccount = new NewAccount
                {
                    //full of populated properties
                };
            _expectedAccount = new Account
                {
                    //matching properties to verify correct mapping
                };

            _mockedRepository
                .Setup(x => x.Create(Moq.It.IsAny<Account>(_account)))
                //here, we capture the actual account passed in.
                .Callback<Account>(x=> _actualAccount = x) 
                .Returns(_account);
        };

    Because of = () => _result = _accountCreator.Create(_newAccount);

    It should_create_the_account_in_the_repository = 
        () => _result.ShouldEqual(_account);

    It should_create_the_expected_account = 
        () => _expectedAccount.ToExpectedObject().ShouldEqual(_actualAccount);
}

期待されるオブジェクトパターンは素晴らしいですが、C#で実装するのは複雑なので、私はそれらすべてを処理するライブラリを使用します。https://github.com/derekgreer/expectedObjects

私の最後の観察では、渡された実際のアカウントのプロパティを調べ、それぞれを「期待されるオブジェクト」の同じプロパティと比較します。このように、私は模擬プロパティチェックの膨大なリストを持っていませんし、テスト観測もたくさんありません。

于 2012-07-05T17:46:44.410 に答える
0

期待されるオブジェクトと実際のオブジェクトJSONConvert.SerializeObjectをJSON文字列に変換し、equalsそれらの間で実行すると、許容できる結果が得られるように見えます。私の考えでは、オブジェクトの文字列表現が一致する場合、それらのパブリックプロパティも同じである可能性が高いと思います。

于 2020-05-07T15:20:17.513 に答える