Beispiel #1
0
        public async Task CreatePostAsync_Called_StoresPostIndDB()
        {
            //Arrange
            //createUser();

            var fluentData = default(FluentData);
            var samplePost = default(Post);

            using (var context = new ApplicationDbContext(inMemoryDatabaseHelper.Options))
            {
                fluentData = new FluentData(context);
                samplePost = fluentData.Generate <Post>();

                var user = fluentData.Generate <ApplicationUser>();
                await fluentData.SaveAsync(user);
            }

            //var samplePost =  await TestDatas.stubs[typeof(Post)]() as Post;

            //Act
            string postId = null;

            using (var context = new ApplicationDbContext(inMemoryDatabaseHelper.Options))
            {
                var user           = context.Users.Single();
                var postRepository = new PostRepository(context);
                var post           = samplePost;

                post.OriginalPoster = user;

                await postRepository.CreatePostAsync(post);

                postId = post.Id.ToString();
            }

            //Assert
            using (var context = new ApplicationDbContext(inMemoryDatabaseHelper.Options))
            {
                var post = context.Posts.Single();

                Assert.AreEqual(1, context.Posts.Count());
                Assert.AreEqual(samplePost.Title, post.Title);
                Assert.AreEqual(samplePost.Content, post.Content);
                Assert.That(post.Id.ToString(), Is.EqualTo(postId.ToString()));

                Assert.That(post.LastUpdated.Date, Is.EqualTo(DateTime.Now.Date));
                Assert.That(post.Created.Date, Is.EqualTo(DateTime.Now.Date));
                Assert.That(post.Created.Date, Is.EqualTo(post.LastUpdated.Date));
            }
        }
Beispiel #2
0
        public async Task Index_ReturnsAViewResult_WithAList()
        {
            //Arrange
            createUser();
            for (int i = 0; i < 2; i++)
            {
                using (var context = new ApplicationDbContext(inMemoryDatabaseHelper.Options))
                {
                    var user           = context.Users.Single();
                    var postRepository = new PostRepository(context);
                    var post           = new Post
                    {
                        Title          = "post",
                        Content        = "post",
                        OriginalPoster = user
                    };
                    await postRepository.CreatePostAsync(post);
                }
            }


            using (var context = new ApplicationDbContext(inMemoryDatabaseHelper.Options))
            {
                // Act
                var postsController = new PostsController(context,
                                                          new Mock <IUserRepository>().Object,
                                                          //new PostRepository(context)
                                                          new Repository <Post>(context)
                                                          );
                var result = await postsController.Index();

                // Assert
                //IsTypeOf
                Assert.IsInstanceOf <ViewResult>(result);
                var viewResult = result as ViewResult;

                //IsAssignableFrom
                Assert.IsInstanceOf <IList <Post> >(viewResult.ViewData.Model);
                var model = viewResult.ViewData.Model;
                Assert.That(model, Is.InstanceOf <IList <Post> >());
                var posts = model as IList <Post>;

                Assert.AreEqual(2, posts.Count());
            }
        }