MS单元测试示例

hoa*_*key 4 unit-testing moq

我正在努力让我的头脑进行单元测试.我一直在关注Nerd晚餐和专业asp.net MVC框架的例子,但是一旦我尝试自己很快就会卡住.作为测试,我尝试构建一个仅使用登录信息的验证类 - usename和password从存储库返回用户.我已经使用了这个例子,因为很容易想象一个人可能会执行哪些测试:Is_Password_Valid,Is_Username_Valid等.

我花了很长时间试图让我的头脑得到这个.任何人都可以举例说明他们如何将其作为单元测试吗?我想,一旦我破解了这个,我就会离开.

//Arrange
string email = "test@test.com";
string password = "test";

//Arrange
List<Customer> customer = new List<Customer>();

customer.Add(new Customer { CustomerId = 1, Email = email, Password = "best", FirstName = "test", LastName = "wods", Sex = true });
mockRepos = new Moq.Mock<ICustomerRepository>();
mockRepos.Setup(x => x.GetCustomerByPasswordUsername(email, password)).Returns(customer.First());
Authenticate auth = new Authenticate(mockRepos.Object);

//Act
var result = auth.Login(email, password);

//Assert
//this is where I start to become unstuck??????
Run Code Online (Sandbox Code Playgroud)

Kla*_*sen 14

看起来你肯定是在正确的轨道上,但让我试着解释一下我将如何进行测试.

这里的实际 被测系统(SUT)就是这个Authenticate类.你没有详细说明,所以我假设如下:

它使用一个实例ICustomerRepository来根据用户名(电子邮件)和密码的组合来确定用户的存在.

当repostiory返回Customer给定用户名和密码组合的实例时,该Login方法返回true.存储库返回时null,该Login方法返回false.

我将在下面使用这些假设,但如果它们不正确,我相信您将能够更改测试,以便它们对您的方案有意义.

测试1:当用户名/密码组合正确时,Login将返回true

public void LoginWillReturnTrueForAValidUsernamePasswordCombination()
{
    string email = "test@test.com";
    string password = "test";

    //Dummy customer
    var customer = new Customer();

    //Create mock
    var mockRepos = new Moq.Mock<ICustomerRepository>();
    mockRepos.Setup(x => x.GetCustomerByPasswordUsername(
            It.Is<string>(s => s == email), 
            It.Is<string>(s => s == password))
        .Returns(customer);

    var auth = new Authenticate(mockRepos.Object);

    //Act
    var result = auth.Login(email, password);

    //Assert
    Assert.IsTrue(result);
}
Run Code Online (Sandbox Code Playgroud)

注意使用It.Is.基本上,模拟的设置方式是,当测试中定义的电子邮件和密码传递给GetCustomerByPasswordUsername方法时,它只返回虚拟客户对象.

测试2:当用户名/密码组合不正确时,Login将返回false

public void LoginWillReturnFalseForAnInvalidUsernamePasswordCombination()
{
    string email = "test@test.com";
    string password = "test";

    //Create mock
    var mockRepos = new Moq.Mock<ICustomerRepository>();
    mockRepos.Setup(x => x.GetCustomerByPasswordUsername(
            It.Is<string>(s => s == email), 
            It.Is<string>(s => s == password))
        .Returns<Customer>(null);

    var auth = new Authenticate(mockRepos.Object);

    //Act
    var result = auth.Login(email, password);

    //Assert
    Assert.IsFalse(result);
}
Run Code Online (Sandbox Code Playgroud)

虽然通过上述测试进行了隐式测试,但您可能希望更进一步,编写一个测试,确保该Login方法将正确的参数传递给存储库.这样的测试可能如下所示:

测试3:登录将正确调用存储库

public void LoginWillInvokeGetCustomerByPasswordUsernameCorrectly()
{
    string email = "test@test.com";
    string password = "test";

    //Create mock
    var mockRepos = new Moq.Mock<ICustomerRepository>();
    mockRepos.Setup(x => x.GetCustomerByPasswordUsername(
            It.Is<string>(s => s == email), 
            It.Is<string>(s => s == password))
        .Returns<Customer>(null)
        .Verifiable();

    var auth = new Authenticate(mockRepos.Object);

    //Act (ignore result. We are only testing correct invocation)
    auth.Login(email, password);

    //Assert
    mockRepos.Verify();
}
Run Code Online (Sandbox Code Playgroud)

Verify一个模拟的方法抛出,如果已安装的方法还没有被称为异常.

我希望这有帮助.如果您有其他问题,请随时询问.