xUnit:使用语句进行安装测试

时间:2019-09-19 13:27:10

标签: c# unit-testing .net-core xunit xunit.net

我正在开发一个Web应用程序,在Web应用程序中,测试也非常重要。我的Web应用程序是在.net Core中构建的,因此我使用xUnit.NET进行了测试

我开始测试帐户控制器的注册操作。测试是:

public class AccountControllerTests
{
    private List<AppUser> users = new List<AppUser>();

    [Fact]
    public async Task TestSuccessFulRegistration()
    {
        UserManager<AppUser> userManager = Mocks.GetUserManager(users);
        SignInManager<AppUser> signInManager = Mocks.GetSignInManager(users);

        AccountController controller = new AccountController(userManager, signInManager, null);

        RegisterUser user = new RegisterUser
        {
            UserName = "testemail@testdomein.tld",
            Email = "testemail@testdomein.tld",
            Password = "Test123@"
        };


        ActionResult<ApiResult> actionResult = await controller.Register(user);

        ApiResult apiResult = actionResult.Value;

        Assert.True(apiResult.Succeeded, "ApiResult should be Succeeded if username and email are unique.");
        Assert.Empty(apiResult.Errors);
        Assert.Empty(apiResult.Message);
        Assert.Null(apiResult.Object);
    }

    [Fact]
    public async Task TestRegistrationWithIncorrectPassword()
    {
        UserManager<AppUser> userManager = Mocks.GetUserManager(users);
        SignInManager<AppUser> signInManager = Mocks.GetSignInManager(users);

        AccountController controller = new AccountController(userManager, signInManager, null);

        RegisterUser user = new RegisterUser
        {
            UserName = "testemail@testdomein.tld",
            Email = "testemail@testdomein.tld",
            Password = "Test"
        };

        ActionResult<ApiResult> actionResult = await controller.Register(user);

        ApiResult apiResult = actionResult.Value;

        Assert.False(apiResult.Succeeded, "Registration should fail if password does not have numbers and special symbols.");
        Assert.NotEmpty(apiResult.Errors);
        Assert.Empty(apiResult.Message);
        Assert.Null(apiResult.Object);
    }
}

我创建了一个静态Mocks类,该类模拟UserManager<Appuser>SignInManager<AppUser>现在,我尝试测试注册。我在应用程序的启动类中设置了密码应包含数字和非字母数字字符。但是,如果我使用test作为密码,则userManager.createAsync返回true。用户管理器是这样嘲笑的:

public static UserManager<TUser> GetUserManager<TUser>(List<TUser> ls) where TUser : class
{
    var store = new Mock<IUserStore<TUser>>();

    var userManager = new Mock<UserManager<TUser>>(store.Object, null, null, null, null, null, null, null, null);
    userManager.Object.UserValidators.Add(new UserValidator<TUser>());
    userManager.Object.PasswordValidators.Add(new PasswordValidator<TUser>());

    userManager.Setup(x => x.DeleteAsync(It.IsAny<TUser>()))
        .ReturnsAsync(IdentityResult.Success);

    userManager.Setup(x => x.CreateAsync(It.IsAny<TUser>(), It.IsAny<string>())).ReturnsAsync(IdentityResult.Success).Callback<TUser, string>((x, y) => ls.Add(x));
    userManager.Setup(x => x.UpdateAsync(It.IsAny<TUser>())).ReturnsAsync(IdentityResult.Success);

    return userManager.Object;
} 

CreateAsync总是会返回成功,但是我什至不知道如何写出IOptions<Identity>被使用。

0 个答案:

没有答案