Esempio n. 1
0
        public void should_create_topic()
        {
            var user            = _app.MockUser();
            var topicController = _app.CreateController <TopicController>();

            var model = new TopicCreationModel()
            {
                Title   = "first topic you created",
                Content = "**This is the content of this markdown**\r\n* markdown content is greate*",
                Type    = TopicType.Job
            };

            var actionResult = topicController.CreateTopic(model);

            var topicCreated = VerifyTopicCreated(actionResult, model);

            topicCreated.LastRepliedAt.ShouldBeNull();
            topicCreated.ReplyCount.ShouldEqual(0);
            topicCreated.ViewCount.ShouldEqual(0);

            var topicCreatedLog = _app.GetLogs().FirstOrDefault(log => log.Message.Contains("创建话题成功"));

            Assert.NotNull(topicCreatedLog);
            Assert.Contains($"UserId = {user.Id}", topicCreatedLog.Message);
            Assert.Contains($"Id = {topicCreated.Id}", topicCreatedLog.Message);
            Assert.Contains(topicCreated.Title, topicCreatedLog.Message);
        }
        public async Task should_use_mvc()
        {
            await server.SendAsync(ctx =>
            {
                ctx.Request.Path = "/";
            });

            var logs = _app.GetLogs();

            logs.ShouldNotBeNull();
            logs.ShouldContain(item => item.Category.StartsWith("Microsoft.AspNetCore.Mvc"));
        }
        async Task should_register_new_user()
        {
            var accountCtrl = _app.CreateController <AccountController>();
            var userName    = "******";
            var newUser     = new UserViewModel
            {
                UserName = userName,
                Password = "******"
            };

            var registerResult = await accountCtrl.DoRegister(newUser);

            registerResult.IsType <RedirectResult>();

            var registeredUser = _userRepo.All().FirstOrDefault(user => user.UserName == newUser.UserName);

            registeredUser.ShouldNotBeNull();
            registeredUser.UserName.ShouldEqual(userName);
            registeredUser.Id.ShouldGreaterThan(0);

            var newUserLog = _app.GetLogs().FirstOrDefault(log => log.Message.Contains("用户注册成功"));

            Assert.NotNull(newUserLog);
            Assert.Contains($"UserId = {registeredUser.Id}", newUserLog.Message);
            Assert.Contains($"UserName = {registeredUser.UserName}", newUserLog.Message);
        }
        public void should_add_reply()
        {
            // Arrange
            var user  = _app.MockUser();
            var topic = _app.NewTopic().WithAuthor(user).Create();


            // Act
            var replyController = _app.CreateController <ReplyController>();

            replyController.Reply(topic.Id, new ReplyCreationModel
            {
                Content = "my reply"
            });

            // Assert
            var replies = _app.GetService <IRepository <Reply> >()
                          .All()
                          .Where(c => c.TopicId == topic.Id)
                          .ToList();

            replies.Count.ShouldEqual(1);
            replies[0].TopicId.ShouldEqual(topic.Id);
            replies[0].CreatedBy.ShouldEqual(user.Id);
            replies[0].Content.ShouldEqual("my reply");

            var dbContext = _app.GetService <ApplicationDbContext>();

            dbContext.Entry(topic).Reload();
            topic.ReplyCount.ShouldEqual(1);
            topic.LastRepliedByUser.ShouldNotBeNull();
            topic.LastRepliedAuthor.ShouldNotBeNull();
            topic.LastRepliedAt.ShouldNotBeNull();
            var span = DateTime.UtcNow - topic.LastRepliedAt.Value;

            Assert.True(span.TotalSeconds > 0);
            Assert.True(span.TotalSeconds < 10);

            var replyCreatedLog = _app.GetLogs().FirstOrDefault(log => log.Message.Contains("添加回复成功"));

            Assert.NotNull(replyCreatedLog);
            Assert.Contains($"UserId = {user.Id}", replyCreatedLog.Message);
            Assert.Contains($"TopicId = {topic.Id}", replyCreatedLog.Message);
            Assert.Contains($"ReplyId = {replies[0].Id}", replyCreatedLog.Message);
        }