Пример #1
0
        public async Task Index_ShouldReturnAllUsersExceptCurrent()
        {
            //Arrange
            var threads = new[]
            {
                new Thread()
                {
                    Id = 1, Name = "First"
                },
                new Thread()
                {
                    Id = 2, Name = "Second"
                },
                new Thread()
                {
                    Id = 3, Name = "Third"
                },
                new Thread()
                {
                    Id = 4, Name = "Fourth"
                },
            };

            var methodCalled = false;

            var models = new[]
            {
                new ThreadConciseViewModel()
                {
                    Id = threads[0].Id, Name = threads[0].Name
                },
                new ThreadConciseViewModel()
                {
                    Id = threads[1].Id, Name = threads[1].Name
                },
                new ThreadConciseViewModel()
                {
                    Id = threads[2].Id, Name = threads[2].Name
                },
                new ThreadConciseViewModel()
                {
                    Id = threads[3].Id, Name = threads[3].Name
                }
            };

            context.Threads.AddRange(threads);
            context.SaveChanges();

            var mockRepository = new Mock <IUserThreadsService>();

            mockRepository
            .Setup(repo => repo.GetThreadsAsync())
            .ReturnsAsync(models)
            .Callback(() => methodCalled = true);

            var controller = new HomeController(mockRepository.Object);

            //Act
            var result = await controller.Index() as ViewResult;

            //Assert
            Assert.IsTrue(methodCalled);
            var resultIds = result.Model as IEnumerable <ThreadConciseViewModel>;

            CollectionAssert.AreEqual(new[] { 1, 2, 3, 4 },
                                      resultIds.Select(u => u.Id).ToArray());
        }
Пример #2
0
        public async Task Index_ShouldReturnPostsAndThreadInstance()
        {
            //Assert
            var mockPostRepository     = new Mock <IUserPostService>();
            var mockInstanceRepository = new Mock <IUserThreadInstancesService>();

            var methodPostCalled     = false;
            var methodInstanceCalled = false;

            var instance = new ThreadInstance()
            {
                Id   = 1,
                Name = "f/First"
            };

            var posts = new[]
            {
                new Post()
                {
                    Id = 1, Name = "First", ThreadInstanceId = instance.Id
                },
                new Post()
                {
                    Id = 2, Name = "Second", ThreadInstanceId = instance.Id
                },
                new Post()
                {
                    Id = 3, Name = "Third", ThreadInstanceId = instance.Id
                },
            };

            var models = new[]
            {
                new PostConciseViewModel()
                {
                    Id = 1, Name = "First"
                },
                new PostConciseViewModel()
                {
                    Id = 2, Name = "Second"
                },
                new PostConciseViewModel()
                {
                    Id = 3, Name = "Third"
                },
            };

            context.ThreadInstances.Add(instance);
            context.SaveChanges();

            context.Posts.AddRange(posts);
            context.SaveChanges();

            mockInstanceRepository
            .Setup(repo => repo.GetInstanceAsync(instance.Id))
            .ReturnsAsync(instance)
            .Callback(() => methodInstanceCalled = true);

            mockPostRepository
            .Setup(repo => repo.GetPostsAsync(instance.Id))
            .ReturnsAsync(models)
            .Callback(() => methodPostCalled = true);

            //Mock http context?
            var indexModel = new IndexModel(mockPostRepository.Object, mockInstanceRepository.Object);

            //Act
            var result = await indexModel.OnGet(instance.Id) as ViewResult;

            //Assert
            Assert.IsTrue(methodInstanceCalled);
            Assert.IsTrue(methodPostCalled);
            Assert.AreEqual(indexModel.ThreadInstanceId, instance.Id);
            Assert.AreEqual(indexModel.ThreadInstanceName, instance.Name.Substring(2, instance.Name.Length - 2));
            CollectionAssert.AreEqual(new[] { 1, 2, 3 }, indexModel.Posts.Select(u => u.Id).ToArray());
        }