我有一个 save() 方法,不太确定如何测试。下面是我的代码。
public interface IRepository<T>
{
T Get(int id);
void Save(T item);
void Delete(int id);
}
save 方法不返回任何值,我无法比较该值。但是,我已经有4个用户了,再添加一个后,我只查看用户总数,测试就够了吗?
[Test]
public void Add_a_new_smoothie_user_should_return_total_5_users()
{
// Arrange
var totalUsers = _users.Count();
_mockUserRepository.Setup(s => s.Save(It.IsAny<User>()))
.Callback((User user) => _users.Add(user));
var newUser = new User
{
Id = 3,
Email = "newuser@test.com",
Password = "1234567".Hash(),
Firstname = "",
Lastname = "",
CreatedDate = DateTime.Now,
LastLogin = DateTime.Now,
AccountType = AccountType.Smoothie,
DisplayName = "",
Avatar = "",
ThirdPartyId = "",
Status = Status.Approved,
Ip = "127.0.0.1"
};
// Act
_mockUserRepository.Object.Save(newUser);
// Assert
Assert.AreEqual(5, _users.Count());
Assert.AreEqual(1, _users.Count() - totalUsers);
}