コード例 #1
0
        public async Task PostUser_ModelInvalid_ReturnsBadRequest()
        {
            // Arrange
            var mockConfiguration = new Mock <Microsoft.Extensions.Configuration.IConfiguration>();
            var mockLogger        = new Mock <ILogger <AccountController> >();
            var mockUmService     = new Mock <IUserManagementService>();
            var mockHelperService = new Mock <IHelperService>();

            var mapperConfig = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new AutomapperProfiles.UserProfile());
            });
            var mapper = mapperConfig.CreateMapper();

            var controller = new UsersController(mockConfiguration.Object, mockLogger.Object,
                                                 mockUmService.Object, mapper, mockHelperService.Object);

            controller.ModelState.AddModelError("LastName", "Last name is missing.");

            var a = new UserModel
            {
                FirstName = "a",
                LastName  = "b",
                Email     = "[email protected]",
            };

            // Act
            var result = await controller.PostUser(a);

            // Assert
            var actionResult = Assert.IsType <ActionResult <UserModel> >(result);
            var returnValue  = Assert.IsType <BadRequestObjectResult>(actionResult.Result);

            mockUmService.Verify();
        }
コード例 #2
0
        public async Task PostUser_NewUserCorrect_UserAddedAndReturned()
        {
            // Arrange
            var mockConfiguration = new Mock <Microsoft.Extensions.Configuration.IConfiguration>();
            var mockLogger        = new Mock <ILogger <AccountController> >();
            var mockUmService     = new Mock <IUserManagementService>();
            var mockHelperService = new Mock <IHelperService>();

            var mapperConfig = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new AutomapperProfiles.UserProfile());
            });
            var mapper = mapperConfig.CreateMapper();

            var controller = new UsersController(mockConfiguration.Object, mockLogger.Object,
                                                 mockUmService.Object, mapper, mockHelperService.Object);

            var a = new UserModel
            {
                FirstName = "a",
                LastName  = "b",
                Email     = "[email protected]",
                Password  = "******",
                Role      = "role"
            };

            mockUmService.Setup(umService => umService.IsEmailInUseAsync("[email protected]"))
            .ReturnsAsync(false)
            .Verifiable();

            ApplicationUser user = mapper.Map <ApplicationUser>(a);

            mockUmService.Setup(umService => umService.AddUserAsync(It.IsAny <ApplicationUser>(), "pass", "role"))
            .ReturnsAsync(new IdentityResult())
            .Verifiable();

            // Act
            var result = await controller.PostUser(a);

            // Assert
            var actionResult          = Assert.IsType <ActionResult <UserModel> >(result);
            var createdAtActionResult = Assert.IsType <CreatedAtActionResult>(actionResult.Result);
            var returnValue           = Assert.IsType <ApplicationUser>(createdAtActionResult.Value);

            mockUmService.Verify();

            Assert.Equal("[email protected]", returnValue.Email);
        }