Example #1
0
        public void Post_OK()
        {
            // Arrange
            var fact = new ChuckNorrisFunFact()
            {
                ModifiedBy   = "Me",
                ModifiedWhen = new DateTime(1000),
                Fact         = "Some Fact",
                Rating       = 10
            };
            var fact2 = new ChuckNorrisFunFact()
            {
                Id           = 2,
                ModifiedBy   = fact.ModifiedBy,
                ModifiedWhen = fact.ModifiedWhen,
                Fact         = fact.Fact,
                Rating       = fact.Rating
            };

            var repository = new Mock <IFunFactsRepository>();

            repository.Setup(ffr => ffr.Add(It.Is <IFunFact>(i => i == fact)))
            .Callback <IFunFact>(f => f.Id = 2)
            .Returns(fact);
            ChuckNorrisFunFactsController controller = new ChuckNorrisFunFactsController(repository.Object);

            // Act
            var response = controller.Post(fact);

            // Assert
            Assert.IsNotNull(response);
            Assert.IsInstanceOfType(response, typeof(CreatedAtRouteNegotiatedContentResult <ChuckNorrisFunFact>));
            var createdRoute = (response as CreatedAtRouteNegotiatedContentResult <ChuckNorrisFunFact>);
            var result       = createdRoute.Content;

            Assert.AreEqual("DefaultApi", createdRoute.RouteName);
            Assert.AreEqual("id", createdRoute.RouteValues.First().Key);
            Assert.AreEqual(2, (long)createdRoute.RouteValues.First().Value);

            var serializer = new JavaScriptSerializer();

            Assert.AreEqual(serializer.Serialize(fact2), serializer.Serialize(result));
            repository.VerifyAll();
        }
Example #2
0
        public void Post_NotValid()
        {
            // Arrange
            var fact = new ChuckNorrisFunFact()
            {
                Id           = 1,
                ModifiedBy   = null,
                ModifiedWhen = new DateTime(1000),
                Fact         = null,
                Rating       = 10
            };

            var repository = new Mock <IFunFactsRepository>();
            ChuckNorrisFunFactsController controller = new ChuckNorrisFunFactsController(repository.Object);

            controller.ModelState.AddModelError("Fact", "test");

            // Act
            var response = controller.Post(fact);

            // Assert
            Assert.IsNotNull(response);
            Assert.IsInstanceOfType(response, typeof(InvalidModelStateResult));
        }