0

我必须在此单元测试中检查重复的电子邮件和用户名。与另一位开发人员交谈时,他说我应该在嵌套的 try catch 中检查重复的电子邮件和用户名。我不确定我是否正确理解了他,但这就是我所拥有的。其他开发者现在不可用,所以我不能要求验证。我认为这种格式不正确。有什么建议吗?

        try
        {

            AsaMembershipProvider prov = this.GetMembershipProvider();


            MembershipCreateStatus status;
            MembershipUser user = prov.CreateUser("UserX", "12345", "UserX@abc.com", "", "", true, null, out status);


            user = prov.GetUser("UserX", false);


            var isAuthenticated = prov.ValidateUser(user.UserName, "12345");
            Assert.IsTrue(isAuthenticated);
            Assert.AreEqual(user.UserName, "UserX");
            Assert.AreEqual(user.Email, "userx@abc.com");

            try
            {

                prov.DeleteUser("UserX", true);

                user = prov.GetUser("UserX", false);

                Assert.AreEqual(null, user);
                user = prov.GetUser("UserX", false);


                Assert.AreEqual(status, MembershipCreateStatus.Success);
            }
            catch (Exception ex)
            {
                LogMessage(ex);
                Assert.Fail(ex.Message);
            }


            prov.DeleteUser("UserX", true);

            user = prov.GetUser("UserX", false);

            Assert.AreEqual(null, user);


        }
        catch (Exception ex)
        {
            LogMessage(ex);
            Assert.Fail(ex.Message);
        }

    }
4

1 回答 1

0

将 try 块中的代码移动到调用Assert.Throws<T>

Assert.Throws<Exception>(() => {
    // attempt to create a duplicate user.
}, "Exception thrown when creating duplicate user.");

更改Exception为您希望在创建副本时引发的确切异常类型。

于 2013-05-15T18:48:07.660 回答