Exemplo n.º 1
0
        public void Create_POST_Does_Not_Save_Changes_If_ModelState_Is_Not_Valid()
        {
            // Arrange
            var controller = new TrackController();
            var repo = new Mock<TestDB>();
            repo.Setup(r => r.SaveChanges()).Throws<Exception>();
            controller._repo = repo.Object;
            controller.ModelState.AddModelError("testError", new Exception());

            // Act
            controller.Create(new TrackModel());

            // Assert
        }
Exemplo n.º 2
0
        public void Create_POST_Redirects_Same_Model_To_GET_If_ModelState_Not_Valid()
        {
            // Arrange
            var controller = new TrackController();
            var track = new TrackModel { Name = "One", Artist = "Metallica" };
            var repo = new Mock<TestDB>();
            controller._repo = repo.Object;
            controller.ModelState.AddModelError("testError", new Exception());

            // Act
            var result = controller.Create(track) as ViewResult;

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual("", result.ViewName);
            Assert.AreEqual(track, result.Model);
        }
Exemplo n.º 3
0
        public void Index_always_returns_IEnumerable_of_TrackModel()
        {
            // Arrange
            var controller = new TrackController();
            var repo = new TestDB();
            repo.Tracks.Add(new Track { Artist = "Metallica", Name = "Battery", Duration = 5.12 });
            repo.Tracks.Add(new Track { Artist = "Nirvana", Name = "Polly", Duration = 2.3 });
            controller._repo = repo;
            // TODO: Mock data mappers
            controller._t2mMapper = new SpotiChelas.DataMappers.TrackToTrackModelMapper();

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

            // Assert
            Assert.IsNotNull(result);
            Assert.IsNotNull(result.Model);
            Assert.IsInstanceOfType(result.Model, typeof(IEnumerable<TrackModel>));
        }
Exemplo n.º 4
0
        public void Create_POST_Saves_Track_If_ModelState_Is_Valid()
        {
            // Arrange
            var controller = new TrackController();
            var track = new TrackModel { Name = "One", Artist = "Metallica" };
            var repo = new Mock<TestDB>();

            controller._repo = repo.Object;
            // TODO: Mock data mappers
            controller._m2tMapper = new SpotiChelas.DataMappers.TrackModelToTrackMapper();

            // Act
            var result = controller.Create(track) as RedirectToRouteResult;

            // Assert
            Assert.IsNotNull(result);
            Assert.AreEqual("action", result.RouteValues.First().Key);
            Assert.AreEqual("Index", result.RouteValues.First().Value);
            Assert.AreEqual(1, repo.Object.Tracks.Count());
            repo.Verify(x => x.SaveChanges());
        }
Exemplo n.º 5
0
        public void Test_Index()
        {
            // Arrange
            var controller = new TrackController();
            var repo = new TestDB();
            repo.Tracks.Add(new Track { Artist = "Metallica", Name = "Battery", Duration = 5.12 });
            repo.Tracks.Add(new Track { Artist = "Nirvana", Name = "Polly", Duration = 2.3 });
            controller._repo = repo;
            // TODO: Mock data mappers
            controller._t2mMapper = new SpotiChelas.DataMappers.TrackToTrackModelMapper();

            // Act
            var result = controller.Index() as ViewResult;
            var list = result.Model as IEnumerable<TrackModel>;

            // Assert
            Assert.AreEqual(2, list.Count());
        }