need help to unit test my service layer in my mvc3 project

asp.net-mvc, asp.net-mvc-3, nunit, unit-testing

Solution

BTW It's better to write tests before you write code. This will allow you to design more handy API, and you will not be limited by implementation details, when writing tests.

Back to your case. You are using mocked repository, thus you don't need to call `Save` to fill repository with some users. Actually you don't need to fill mock at all. You should simply return values, which are required for your test scenario.

[Test]
public void ShouldSuccesfulltyAddNonExistingUser()
{
   // Arrrange
   int userId = 5;
   var user = new User { Email = "newuser@test.com", Password = "1234567".Hash() };
   _mockUserRepository.Setup(r => r.GetUserByEmail(user.Email, AccountType.Smoothie)).Returns(null);
   _mockUserRepository.Setup(r => r.Save(user)).Returns(userId);
   _userService = new UserService(_mockUserRepository.Object);

   // Act
   ActionConfirmation<User> confirmation = _userService.AddUser(user);

   // Assert       
   Assert.True(confirmation.WasSuccessful);
   Assert.That(confirmation.Message, Is.EqualTo(""));
   Assert.That(confirmation.Value, Is.EqualTo(user));
   Assert.That(confirmation.Value.Id, Is.EqualTo(userId));
}

Keep in mind, that you should not provide user id, when you are creating new user. Id should be assigned after user saved to repository.

Problem

my mvc3 project has service and repository layers. my service layer: ``` public class UserService : IUserService { private readonly IUserRepository _userRepository; public UserService(IUserRepository userRepository) { _userRepository = userRepository; } public ActionConfirmation<User> AddUser(User user) { User existUser = _userRepository.GetUserByEmail(user.Email, AccountType.Smoothie); ActionConfirmation<User> confirmation; if (existUser != null) { confirmation = new ActionConfirmation<User>() { WasSuccessful = false, Message = "This Email already exists", Value = null }; } else { int userId = _userRepository.Save(user); user.Id = userId; confirmation = new ActionConfirmation<User>() { WasSuccessful = true, Message = "", Value = user }; } return confirmation; } } ``` here's my Unit test, not sure how to do the act and assert. please help me out, if you need code from other layers, please let me know. I will put them here. I think this should be enough. ``` [TestFixture] public class UserServiceTests { private UserService _userService; private List<User> _users; private Mock<IUserRepository> _mockUserRepository; [SetUp] public void SetUp() { _mockUserRepository = new Mock<IUserRepository>(); _users = new List<User> { new User { Id = 1, Email = "test@hotmail.com", Password = "" }, new User { Id = 1, Email = "test2@hotmail.com", Password = "123456".Hash() }, new User { Id = 2, Email = "9422722@twitter.com", Password = "" }, new User { Id = 3, Email = "john.test@test.com", Password = "12345".Hash() } }; } [Test] public void AddUser_adding_a_nonexist_user_should_return_success_confirmation() { // Arrange _mockUserRepository.Setup(s => s.Save(It.IsAny<User>())).Callback((User user) => _users.Add(user)); var newUser = new User { Id = 4, Email = "newuser@test.com", Password = "1234567".Hash() }; _userService = new UserService(_mockUserRepository.Object); // Act // Assert } } ```

Original source