【发布时间】:2009-07-03 16:54:29
【问题描述】:
我有一个要进行单元测试的方法 CreateAccount(...)。基本上它创建一个帐户实体并将其保存到数据库中,然后返回新创建的帐户。我正在嘲笑存储库并期待一个 Insert(...) 调用。但是 Insert 方法需要一个 Account 对象。
这个测试通过了,但它似乎不正确,因为 CreateAccount 创建了一个帐户,而我正在为模拟的预期调用创建一个帐户(两个单独的 Account 实例)。测试这种方法的正确方法是什么?还是我使用这种方法创建帐户不正确?
[Fact]
public void can_create_account()
{
const string email = "test@asdf.com";
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;
}
【问题讨论】:
标签: unit-testing tdd rhino-mocks