public async Task Index_should_return_default_view()
        {
            var controller = new BillsController(_applicationDbContext);
            var viewResult = await controller.Index();

            var viewName = ((ViewResult)viewResult).ViewName;

            Assert.True(string.IsNullOrEmpty(viewName) || viewName == "Index");
        }
示例#2
0
        public void IndexReturnsBillsListToView()
        {
            //arrange
            controller = new BillsController(mockBillService.Object, null, null);
            //act
            var result = ((controller.Index() as ViewResult).Model) as List <CreateBillVM>;

            //assert
            Assert.AreEqual(result.Count, 3);
        }
        public async Task Index_should_return_list()
        {
            var controller = new BillsController(_applicationDbContext);
            var viewResult = await controller.Index();

            var         model = ((ViewResult)viewResult).Model;
            List <Bill> bills = ((BillsViewModel)model).bills;

            Assert.IsType <List <Bill> >(bills);
            Assert.True(bills.Count > 0, "List of bills");
            Assert.Equal(bills.Count, 10);
            Assert.Equal(bills[0].ID, 1);
        }
        public void Index_ViewResult()
        {
            // Arrange
            int userId = 1;
            IEnumerable <Bill> bills = new List <Bill>
            {
                new Bill {
                    Id = 1
                }, new Bill {
                    Id = 2
                }
            };
            int totalRecordsAmount = bills.Count();

            IEnumerable <User> users = new List <User>
            {
                new User {
                    Id = 1
                }, new User {
                    Id = 2
                }
            };

            Mock <GenericRepository <Bill> > mockBillRepository = new Mock <GenericRepository <Bill> >();

            mockBillRepository
            .Setup(br => br.Count(It.IsAny <Expression <Func <Bill, bool> > >()))
            .Returns(totalRecordsAmount);
            mockBillRepository
            .Setup(br => br.Get(It.IsAny <Expression <Func <Bill, bool> > >(),
                                It.IsAny <Func <IQueryable <Bill>, IOrderedQueryable <Bill> > >(),
                                It.IsAny <string>(), It.IsAny <int>(),
                                It.IsAny <int>()))
            .Returns(bills);

            Mock <UserRepository> mockUserRepository = new Mock <UserRepository>();

            mockUserRepository
            .Setup(ur => ur.Get(It.IsAny <Expression <Func <User, bool> > >(), null, It.IsAny <string>(), null, null))
            .Returns(users);

            Mock <IUnitOfWork> mockIUnitOfWork = new Mock <IUnitOfWork>();

            mockIUnitOfWork
            .Setup(u => u.GetRepository <Bill, GenericRepository <Bill> >())
            .Returns(mockBillRepository.Object);
            mockIUnitOfWork
            .Setup(u => u.GetRepository <User, UserRepository>())
            .Returns(mockUserRepository.Object);

            BillsController controller = new BillsController(mockIUnitOfWork.Object);

            Mock <ClaimsPrincipal> mockManager = new Mock <ClaimsPrincipal>();

            mockManager
            .Setup(p => p.FindFirst(It.IsAny <string>()))
            .Returns(new Claim(nameof(User.Id), userId.ToString()));

            controller.ControllerContext = new ControllerContext
            {
                HttpContext = new DefaultHttpContext()
                {
                    User = mockManager.Object
                }
            };

            // Act
            IActionResult result = controller.Index(null, null);

            // Assert
            Assert.NotNull(result);
            ViewResult viewResult = Assert.IsType <ViewResult>(result);

            Assert.NotNull(viewResult.Model);
            IndexViewModel indexViewModel = Assert.IsType <IndexViewModel>(viewResult.Model);

            Assert.Same(bills, indexViewModel.Bills);
            Assert.Same(users, indexViewModel.Renters);
            Assert.Equal(totalRecordsAmount, indexViewModel.TotalRecordsAmount);
            Assert.Null(viewResult.ViewName);
        }