使用起订量来验证方法被调用的库(Using moq to verify a method is ca

2019-10-17 08:53发布

我问这个问题,前面有关测试的控制器操作和验证,在我仓库里的方法被调用。 答案回来,我应该测试Save方法被称为在仅在仓库一个单独的测试注册方法(无论是在同一仓库)内。 这就是我想,但我来了做测试,我无法得到它的工作。 :(

这里的仓库测试,我要去哪里错了?

    [TestMethod]
    public void Register_calls_Save_method_when_Member_is_valid()
    {
        _mockMemberRepository.Setup(r => r.GetByEmail(It.IsAny<string>())).Returns((Member)null);            
        MembershipCreateStatus status = _mockMemberRepository.Object.Register(_testMember.Email, "password", "password");
        _mockMemberRepository.Verify(r => r.Save(It.IsAny<Member>()), Times.Once());
    }

下面是对资源库中的注册方法:

public MembershipCreateStatus Register(string email, string password, string confirm)
    {
        if (password.Equals(confirm))
        {
            try
            {
                Member m = GetByEmail(email);
                if (m == null)
                {
                    int format = (int)PasswordFormatEnum.Encrypted;
                    string salt = GenerateSalt();
                    string pass = EncodePassword(password, format, salt);

                    m = new Member()
                    {
                        Email = email,
                        Password = pass,
                        PasswordSalt = salt,
                        PasswordFormat = format
                    };
                    Save(m);
                    return MembershipCreateStatus.Success;
                }
                else
                    return MembershipCreateStatus.DuplicateEmail;
                //"A user with that email address already exists. Please use the Forgotten Password link if you need to recover your password.";
            }
            catch (Exception ex)
            {
                _logger.LogError(ex);
                return MembershipCreateStatus.ProviderError;
            }
        }
        return MembershipCreateStatus.InvalidPassword;
    }

Answer 1:

不能使用最小起订量来验证你从该对象上的另一种方法调用的对象上一个方法。 你可以做的是验证的东西,叫你Save()方法被调用。

例如,如果我写的是用Ado.Net更新数据库我自己的仓库,我可以做类似如下:

public class MyRepository : IRepository {
   private readonly IDatabase m_db;
   public MyRepository(IDatabase myDatabase){
      m_db = myDatabase;
   }

   public void Register(string email, string password, etc.){
      // ... do stuff ...
      Save(m);
      // ... do stuff ...
   }

   public void Save(Member member){
      // ... build sql query ...
      m_db.ExecuteNonQuery(sqlCommand);

   }
}

然后你会传递一个嘲笑数据库对象存储库在您的测试,你会验证:

[TestMethod]
public void Register_calls_Save_method_when_Member_is_valid()
{
    Mock<IDatabase> _mockDB = new Mock<IDatabase>();
    // Setup mockDB with return values for GetByEmail(), etc.

    _repository = new MyRepository(_mockDB.Object);

    MembershipCreateStatus status = _repository.Register("Email@Email.com", "password", "password");
    _mockDB.Verify(r => r.ExecuteNonQuery(It.IsAny<SqlCommand>()), Times.Once());
}

所以,你不验证保存()显式调用,而是通过验证正确的底层数据库调用被称为可以验证保存()发生。

同样的方法应该对其他框架工作了。



文章来源: Using moq to verify a method is called in a repository