예제 #1
0
        public void Create_ShouldExecuteCorrectly()
        {
            var user = UserCreator.Create("test");
            var list = new List <Topic>();

            var topicRepo = DeletableEntityRepositoryMock.Get <Topic>(list);
            var service   = new TopicService(topicRepo.Object);

            service.CreateAsync(user.Id, string.Empty, string.Empty).Wait();

            Assert.Single(list);
        }
예제 #2
0
        public async Task <ActionResult> Create(TopicEditModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    Topic topic = await topicService.CreateAsync(model);

                    return(this.Json(AjaxResult.CreateByContext(topic)));
                }
                catch (ModelException ex)
                {
                    return(this.Json(ex.ToAjaxResult()));
                }
                catch (Exception ex)
                {
                    return(this.Json(AjaxResult.CreateByMessage(ex.Message, false)));
                }
            }
            else
            {
                return(this.Json(ModelState.ToAjaxResult()));
            }
        }
예제 #3
0
        public async Task Should_create_new_topic_and_add_event()
        {
            var options = Shared.CreateContextOptions();

            var categoryId = Guid.NewGuid();
            var forumId    = Guid.NewGuid();
            var userId     = Guid.NewGuid();

            var category = new Category(categoryId, Guid.NewGuid(), "Category", 1, Guid.NewGuid());
            var forum    = new Forum(forumId, category.Id, "Forum", "my-forum", "My Forum", 1);
            var user     = new User(userId, Guid.NewGuid().ToString(), "Email", "Display Name");

            using (var dbContext = new AtlasDbContext(options))
            {
                dbContext.Categories.Add(category);
                dbContext.Forums.Add(forum);
                dbContext.Users.Add(user);
                await dbContext.SaveChangesAsync();
            }

            using (var dbContext = new AtlasDbContext(options))
            {
                var command = Fixture.Build <CreateTopic>()
                              .With(x => x.ForumId, forum.Id)
                              .With(x => x.UserId, userId)
                              .Create();

                var cacheManager = new Mock <ICacheManager>();

                var createValidator = new Mock <IValidator <CreateTopic> >();
                createValidator
                .Setup(x => x.ValidateAsync(command, new CancellationToken()))
                .ReturnsAsync(new ValidationResult());

                var updateValidator = new Mock <IValidator <UpdateTopic> >();

                var sut = new TopicService(dbContext,
                                           cacheManager.Object,
                                           createValidator.Object,
                                           updateValidator.Object);

                await sut.CreateAsync(command);

                var topic = await dbContext.Posts.FirstOrDefaultAsync(x => x.Id == command.Id);

                var @event = await dbContext.Events.FirstOrDefaultAsync(x => x.TargetId == command.Id);

                var updatedCategory = await dbContext.Categories.FirstOrDefaultAsync(x => x.Id == category.Id);

                var updatedForum = await dbContext.Forums.FirstOrDefaultAsync(x => x.Id == forum.Id);

                var updatedUser = await dbContext.Users.FirstOrDefaultAsync(x => x.Id == userId);

                createValidator.Verify(x => x.ValidateAsync(command, new CancellationToken()));
                Assert.NotNull(topic);
                Assert.NotNull(@event);
                Assert.AreEqual(category.TopicsCount + 1, updatedCategory.TopicsCount);
                Assert.AreEqual(topic.Id, updatedForum.LastPostId);
                Assert.AreEqual(forum.TopicsCount + 1, updatedForum.TopicsCount);
                Assert.AreEqual(forum.TopicsCount + 1, updatedForum.TopicsCount);
                Assert.AreEqual(user.TopicsCount + 1, updatedUser.TopicsCount);
            }
        }