单元测试以验证密码

时间:2013-05-10 18:26:57

标签: c# unit-testing validation passwords

有人可以为验证用户密码的单元测试提供一些示例代码吗?

            //set test to crate user 
            AsaMembershipProvider prov = this.GetMembershipProvider();
            //call get user
            MembershipCreateStatus status;
            MembershipUser user = prov.CreateUser("testUserX", "12345", "test.UserX@abc.com", "", "", true, null, out status);
            user = prov.GetUser("testUserX", false);



            //TODO Asserts
            Assert.AreEqual(status, MembershipCreateStatus.Success);
            Assert.AreEqual(user.UserName, "testUserX");
            Assert.AreEqual(user.Email, "test.userx@abc.com");
            //Assert.AreEqual(password, "12345");

1 个答案:

答案 0 :(得分:2)

如果要断言密码正确,请断言您能够进行身份验证,而不是直接测试密码。测试提供程序的功能有点多余。

 Assert.AreEqual(status, MembershipCreateStatus.Success);

 var isAuthenticated = Membership.ValidateUser(user.Username, "12345");

 Assert.IsTrue(isAuthenticated);
 Assert.AreEqual(user.UserName, "testUserX");
 Assert.AreEqual(user.Email, "test.userx@abc.com");