我的 mvc3 项目有服务和存储库层。
我的服务层:
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;
}
}
这是我的单元测试,不知道如何执行和断言。请帮帮我,如果您需要其他层的代码,请告诉我。我会把它们放在这里。我认为这应该足够了。
[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
}
}