は、私はユニットテストしたいメソッドCreateAccount(...)を持っています。基本的に、Account Entityを作成してDBに保存し、新しく作成したAccountを返します。私はリポジトリを嘲笑して、Insert(...)呼び出しを期待しています。しかし、InsertメソッドはAccountオブジェクトを必要とします。が正しくユニットテストサービス/リポジトリの相互作用
このテストはパスするが、CreateAccountがアカウントを作成するので、それだけで、正しいいないようだ、と私はMock'ed期待コール(2つの別々のアカウントインスタンス)のアカウントを作成しています。この方法をテストする正しい方法は何でしょうか?それとも、この方法を使ってアカウントを作成しているのですか?
[Fact]
public void can_create_account()
{
const string email = "[email protected]";
const string password = "password";
var accounts = MockRepository.GenerateMock<IAccountRepository>();
accounts.Expect(x => x.Insert(new Account()));
var service = new AccountService(accounts);
var account = service.CreateAccount(email, password, string.Empty, string.Empty, string.Empty);
accounts.VerifyAllExpectations();
Assert.Equal(account.EmailAddress, email);
}
そしてここCreateAccount方法です:
public Account CreateAccount(string email, string password, string firstname, string lastname, string phone)
{
var account = new Account
{
EmailAddress = email,
Password = password,
FirstName = firstname,
LastName = lastname,
Phone = phone
};
accounts.Insert(account);
return account;
}
申し訳ありませんが、私は当たり前、私がテストしていた実際の方法を掲載している必要があります。私はOPを更新しました。 – mxmissile
私は2つの別々のインスタンスが作成されているのでnewAccとアカウントが同じになる方法を見てはいけません。またはAreSame()は型を比較するだけですか? – mxmissile
正しい。 AreSameはオブジェクトのインスタンスを比較して、元の私の答えが正しくないようにします。私は以来、いくつかの修正を追加しました(上に) – jpoh