public void IndexActionReturnsListOfUsers()
        {
            // Arrange

            // create the mock
            var mockRepository = new Mock<IUserRepository>();

            // create a list of users to return
            var users = new[] {
                new User { username = "******", firstname = "fn1", lastname = "ln1", password = "******", address = "ad1", datejoined = DateTime.Now },
                new User { username = "******", firstname = "fn2", lastname = "ln2", password = "******", address = "ad2", datejoined = DateTime.Now },
                new User { username = "******", firstname = "fn3", lastname = "ln3", password = "******", address = "ad3", datejoined = DateTime.Now }
            };

            // tell the mock that when GetAll is called,
            // return the list of users
            mockRepository.Setup(r => r.GetAll()).Returns(users);

            // pass the mocked instance, not the mock itself, to the category
            // controller using the Object property
            var controller = new UserController(mockRepository.Object);

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

            // Assert
            Assert.IsInstanceOfType(
                result.ViewData.Model,
                typeof(IEnumerable<User>),
                "View model is wrong type");
            var model = result.ViewData.Model as IEnumerable<User>;
            Assert.AreEqual(model.Count(), 3, "Incorrect number of results");
            Assert.AreEqual(model.AsQueryable<User>().FirstOrDefault<User>()
                .username, "u1", "Incorrect data in result");
        }
        public void IndexActionReturnsErrorViewIfNoData()
        {
            // Arrange

            // create the mock
            var mockRepository = new Mock<IUserRepository>();

            // create an empty list of users to return
            var users = new List<User>();

            // tell the mock that when GetAll is called,
            // return the list of users
            mockRepository.Setup(r => r.GetAll()).Returns(users);

            // pass the mocked instance, not the mock itself, to the category
            // controller using the Object property
            var controller = new UserController(mockRepository.Object);

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

            // Assert
            Assert.AreEqual("Error", result.ViewName, "Incorrect view");
            Assert.AreEqual(false, result.ViewData.ModelState.IsValid);
        }