I have a save() method, not really sure how to test. below is my code.
public interface IRepository<T>
{
T Get(int id);
void Save(T item);
void Delete(int id);
}
save method doesn’t return any values back, I cannot compare the value. however, I already have 4 users, after adding another one, I only check the total number of users, is it enough to test it?
[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);
}
You are mocking the part of the functionality you are trying to test. These tests will prove nothing, any other than
Add()method of the data type you are holding the users. In the end it doesn’t give any ideas if your repository is working.You should try to implement a Database Sandbox for testing your repository functionality.