Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing AccountController ASP.NET MVC5的Unittest注册操作_Unit Testing_Asp.net Identity - Fatal编程技术网

Unit testing AccountController ASP.NET MVC5的Unittest注册操作

Unit testing AccountController ASP.NET MVC5的Unittest注册操作,unit-testing,asp.net-identity,Unit Testing,Asp.net Identity,我想为帐户控制器的注册操作编写单元测试,在谷歌搜索和堆栈溢出之后,我想到了这个,请查看这个 public void AccountController_Register_NewAccount() { // Arrange var registerViewmodel = new RegisterViewModel { Email = "mo

我想为帐户控制器的注册操作编写单元测试,在谷歌搜索和堆栈溢出之后,我想到了这个,请查看这个

public void AccountController_Register_NewAccount()
            {
                // Arrange
                var registerViewmodel = new RegisterViewModel
                {
                    Email = "mohtshm@gmail.com",
                    Password = "P123456a*",
                    ConfirmPassword = "P123456a*"
                };

                var appUser = new ApplicationUser
                {
                    Email = "mohtshm@gmail.com",
                    UserName = "mohtshm@gmail.com"
                };


                HttpContext.Current = CreateHttpContext(userLoggedIn: true);
                var IuserStore = new Mock<IUserStore<ApplicationUser>>();
                IdentityResult resultCreateAsync = new IdentityResult(new string[] { });

                var userStore = IuserStore.Object;
                var userManager = new Mock<ApplicationUserManager>(userStore);
                userManager.Setup(u => u.CreateAsync(appUser, registerViewmodel.Password)).ReturnsAsync(resultCreateAsync);
                var authenticationManager = new Mock<IAuthenticationManager>();
                var signInManager = new Mock<ApplicationSignInManager>(userManager.Object, authenticationManager.Object);

                var accountController = new AccountController(
                    userManager.Object, signInManager.Object);
                accountController.AuthenticationManager = authenticationManager.Object;
                // Act
                var result =
                accountController.Register(registerViewmodel).Result;
                Assert.IsNotNull(result);
                var addedUser = userStore.FindByIdAsync("mohtshm@gmail.com").Result;
                Assert.IsNotNull(addedUser);
                Assert.AreEqual("mohtshm@gmail.com", addedUser.UserName);

                // Assert
                //Assert.That(result, Is.TypeOf<RedirectToRouteResult>());
            }
  [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    return RedirectToAction("Index", "Home");
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
    // Modified this from private to public and add the setter
    public IAuthenticationManager AuthenticationManager
    {
        get
        {
            if (_authnManager == null)
                _authnManager = HttpContext.GetOwinContext().Authentication;
            return _authnManager;
        }
        set { _authnManager = value; }
    }