public void Put_ConcurrencyNotExist() { // Arrange var fact = new ChuckNorrisFunFact() { Id = 1, ModifiedBy = null, ModifiedWhen = new DateTime(1000), Fact = null, Rating = 10 }; var repository = new Mock <IFunFactsRepository>(); repository.Setup(ffr => ffr.Update(It.Is <IFunFact>(i => i == fact))).Throws <DbUpdateConcurrencyException>(); repository.Setup(ffr => ffr.Get(It.Is <long>(i => i == 1))).Returns <IFunFact>(null); ChuckNorrisFunFactsController controller = new ChuckNorrisFunFactsController(repository.Object); // Act var response = controller.Put(1, fact); // Assert Assert.IsNotNull(response); Assert.IsInstanceOfType(response, typeof(NotFoundResult)); }
public void Put_Null() { // Arrange var repository = new Mock <IFunFactsRepository>(); ChuckNorrisFunFactsController controller = new ChuckNorrisFunFactsController(repository.Object); // Act var response = controller.Put(1, null); // Assert Assert.IsNotNull(response); Assert.IsInstanceOfType(response, typeof(BadRequestErrorMessageResult)); }
public void Put_Id() { // 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); // Act var response = controller.Put(2, fact); // Assert Assert.IsNotNull(response); Assert.IsInstanceOfType(response, typeof(BadRequestResult)); }
public void Put_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.Put(1, fact); // Assert Assert.IsNotNull(response); Assert.IsInstanceOfType(response, typeof(InvalidModelStateResult)); }
public void Put_OK() { // Arrange var fact = new ChuckNorrisFunFact() { Id = 1, Fact = "Some Fact", Rating = 10 }; var fact2 = new ChuckNorrisFunFact() { Id = 1, Fact = "Some Fact", ModifiedBy = "Me", ModifiedWhen = DateTime.Now, Rating = 10 }; var repository = new Mock <IFunFactsRepository>(); repository.Setup(ffr => ffr.Update(It.Is <IFunFact>(i => i == fact))).Returns(fact2); ChuckNorrisFunFactsController controller = new ChuckNorrisFunFactsController(repository.Object); // Act var response = controller.Put(1, fact); // Assert Assert.IsNotNull(response); Assert.IsInstanceOfType(response, typeof(OkNegotiatedContentResult <IFunFact>)); var result = (response as OkNegotiatedContentResult <IFunFact>).Content; var serializer = new JavaScriptSerializer(); Assert.AreEqual(fact2, result); repository.VerifyAll(); }