コード例 #1
0
        public async void new_should_redirect_if_model_is_valid()
        {
            // Arrange
            const string actionName = "New";
            var validModel = new UserModel { Id = 1, FullName = "test", Password = "******", Email = "*****@*****.**", Language = Thread.CurrentThread.CurrentUICulture.Name };

            var userService = new Mock<IUserService>();
            userService.Setup(x => x.Create(validModel.FullName, validModel.Email, validModel.Password, SetRole.User.Value, validModel.Language))
                       .Returns(Task.FromResult<long?>(1));

            var formsAuthenticationService = new Mock<IFormsAuthenticationService>();
            formsAuthenticationService.Setup(x => x.SignIn(validModel.Id, validModel.FullName, validModel.Email, SetRole.User.Value, true));

            // Act
            var sut = new UserControllerBuilder().WithUserService(userService.Object)
                                                 .WithFormsAuthenticationService(formsAuthenticationService.Object)
                                                 .Build();
            var view = await sut.New(validModel) as RedirectResult;

            // Assert
            Assert.NotNull(view);
            Assert.AreEqual(view.Url, "/user/detail");
            Assert.IsInstanceOf<BaseController>(sut); 

            sut.AssertPostAttribute(actionName, new[] { typeof(UserModel) });
            sut.AssertAllowAnonymousAttribute(actionName, new[] { typeof(UserModel) });

            userService.Verify(x => x.Create(validModel.FullName, validModel.Email, validModel.Password, SetRole.User.Value, validModel.Language), Times.Once);
            formsAuthenticationService.Verify(x => x.SignIn(validModel.Id, validModel.FullName, validModel.Email, SetRole.User.Value, true), Times.Once);
        }
コード例 #2
0
        public void new_should_return_with_user_model()
        {
            // Arrange
            const string actionName = "New";

            // Act
            var sut = new UserControllerBuilder().Build();
            var view = sut.New();

            // Assert
            Assert.NotNull(view);
            Assert.NotNull(view.Model);
            Assert.IsInstanceOf<BaseController>(sut);
            Assert.IsAssignableFrom<UserModel>(view.Model);

            sut.AssertGetAttribute(actionName);
            sut.AssertAllowAnonymousAttribute(actionName);
        }