public void Add_blogarticle_when_title_is_having_and_content_is_having_should_add_a_record_blogarticle() { //Arrange var articleRepositoryMocker = new Mock <IBlogArticleRepository>(); var currentTimeProviderMocker = new Mock <ICurrentTimeProvider>(); var unitOfWorkFactoryMocker = new Mock <IUnitOfWorkFactory>(); var work = new AddWorkCommand { Title = "1", Category = Enums.Category.Web前段, Content = "2" }; LoginUserSection.Start(new LoginUserInformationForCodeSection { LoginName = "yzuhao" }); var datetime = new DateTime(2018, 01, 01); currentTimeProviderMocker.Setup(x => x.CurrentTime()).Returns(datetime); var blogArticleLogic = new BlogArticleLogic(articleRepositoryMocker.Object, currentTimeProviderMocker.Object, unitOfWorkFactoryMocker.Object); //Act blogArticleLogic.Add(work); //Assert articleRepositoryMocker.Verify(x => x.Add(It.IsAny <BlogArticle>()), Times.Once); }
/// <summary> /// Helper method to encapsulate exception handling for WebAPI /// </summary> /// <typeparam name="TResult"></typeparam> /// <param name="code"></param> /// <returns></returns> protected TResult Execute <TResult>(Func <TResult> code) { try { if (CurrentUser != null) { using (var session = LoginUserSection.Start(CurrentUser)) { return(ExecuteManager.Execute(code)); } } else { return(ExecuteManager.Execute(code)); } } catch (UnauthorizedException e) { HttpResponseMessage response = this.Request.CreateErrorResponse(HttpStatusCode.Unauthorized, e.Message); throw new HttpResponseException(response); } catch (DomainException e) { HttpResponseMessage response = this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e.Message); throw new HttpResponseException(response); } catch (Exception) { HttpResponseMessage response = this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ErrorMessage.InternalServerError); throw new HttpResponseException(response); } }
public void Update_blogarticle_when_title_is_having_and_content_is_having_should_update_blogarticle() { //Arrange var articleRepositoryMocker = new Mock <IBlogArticleRepository>(); var currentTimeProviderMocker = new Mock <ICurrentTimeProvider>(); var unitOfWorkFactoryMocker = new Mock <IUnitOfWorkFactory>(); var work = new UpdateWorkCommand { Id = 6, Title = "3", Category = Enums.Category.Web前段, Content = "3" }; LoginUserSection.Start(new LoginUserInformationForCodeSection { LoginName = "yzuhao" }); var datetime = new DateTime(2018, 01, 01); currentTimeProviderMocker.Setup(x => x.CurrentTime()).Returns(datetime); var info = new BlogArticle { Id = 6, Title = "1", Category = Enums.Category.Web前段, Content = "2", CreateUser = "******", CreateDate = datetime }; articleRepositoryMocker.Setup(x => x.Get(It.IsAny <int>())).Returns(info); var blogArticleLogic = new BlogArticleLogic(articleRepositoryMocker.Object, currentTimeProviderMocker.Object, unitOfWorkFactoryMocker.Object); //Act blogArticleLogic.Update(work); //Assert info.Title.Should().Be("3"); info.Category.Should().Be(Enums.Category.Web前段); info.Content.Should().Be("3"); }
protected override void OnActionExecuting(ActionExecutingContext filterContext) { if (LoginUser == null) { base.OnActionExecuting(filterContext); } else { //var actionName = filterContext.ActionDescriptor.ActionName; //var controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName; //var roleFunctions = LoginUser.RoleAndFunctions.Where(x => x.ControllerName == controllerName); _loginUserSection = LoginUserSection.Start(new LoginUserInformationForCodeSection { SystemUserId = LoginUser.SystemUserId, LoginName = LoginUser.LoginName }); base.OnActionExecuting(filterContext); } }
public void LogicDelete_blogarticle_when_need_not_should_delete_this_record_by_id() { //Arrange var articleRepositoryMocker = new Mock <IBlogArticleRepository>(); var currentTimeProviderMocker = new Mock <ICurrentTimeProvider>(); var unitOfWorkFactoryMocker = new Mock <IUnitOfWorkFactory>(); const int id = 6; LoginUserSection.Start(new LoginUserInformationForCodeSection { LoginName = "yzuhao" }); var datetime = new DateTime(2018, 01, 01); currentTimeProviderMocker.Setup(x => x.CurrentTime()).Returns(datetime); var info = new BlogArticle { Title = "1", Category = Enums.Category.Web前段, Content = "2", CreateUser = "******", CreateDate = datetime }; articleRepositoryMocker.Setup(x => x.Get(It.IsAny <int>())).Returns(info); unitOfWorkFactoryMocker.Setup(x => x.GetCurrentUnitOfWork().Commit()); var blogArticleLogic = new BlogArticleLogic(articleRepositoryMocker.Object, currentTimeProviderMocker.Object, unitOfWorkFactoryMocker.Object); //Act blogArticleLogic.LogicDelete(id); //Assert info.Title.Should().Be("1"); info.Status.Should().Be(Enums.Status.Delete); info.EditUser.Should().Be("yzuhao"); info.EditDate.Should().Be(datetime); }
public static LoginUserSection Start(LoginUserInformationForCodeSection systemUser) { var section = new LoginUserSection(systemUser); return(section); }