3

私の 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

    }

}
4

3 に答える 3

5

ところで、コードを書く前にテストを書いたほうがいいです。これにより、より便利な API を設計できるようになり、テストを作成するときに実装の詳細に制限されることがなくなります。

あなたのケースに戻ります。Saveモックされたリポジトリを使用しているため、一部のユーザーでリポジトリを埋めるために呼び出す必要はありません。実際には、モックを埋める必要はまったくありません。テストシナリオに必要な値を返すだけです。

[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));
}

新しいユーザーを作成するときは、ユーザー ID を指定しないでください。ユーザーがリポジトリに保存された後、ID を割り当てる必要があります。

于 2012-07-18T09:21:07.097 に答える
0

まず、ユーザーが既に存在するかどうかに基づいて返される確認オブジェクトをテストします。確認したいもう 1 つのことは_userRepository.Save、ユーザーが存在しないときに が呼び出されることです。

于 2012-07-17T20:25:18.783 に答える
0
// Act
var result = _userService.AddUser(newUser);
// Assert
Assert.IsTrue( /* Insert some condition about the result */ );
Assert.IsTrue( /* Rinse, wash, repeat */ );
于 2012-07-17T20:27:27.840 に答える