public void PostBug_WhenBugTextIsValid_ShouldBeAddedToRepoWithDateCreatedAndStatusNew() { // Arrange var repo = new RepositoryMock<Bug>(); repo.IsSaveCalled = false; repo.Entities = new List<Bug>(); var newBug = new Bug() { Text = "Test bug" }; var controller = new BugsController(repo); this.SetupController(controller, "bugs"); // Act var httpResponse = controller.PostBug(newBug).ExecuteAsync(new CancellationToken()).Result; // Assert Assert.AreEqual(HttpStatusCode.Created, httpResponse.StatusCode); Assert.IsNotNull(httpResponse.Headers.Location); var bugFromService = httpResponse.Content.ReadAsAsync<Bug>().Result; Assert.AreEqual(bugFromService.Text, newBug.Text); Assert.AreEqual(bugFromService.Status, BugStatus.New); Assert.IsNotNull(bugFromService.DateCreated); // Assert the repository values are correct Assert.AreEqual(repo.Entities.Count, 1); var bugInRepo = repo.Entities.First(); Assert.AreEqual(newBug.Text, bugInRepo.Text); Assert.IsNotNull(bugInRepo.DateCreated); Assert.AreEqual(BugStatus.New, bugInRepo.Status); Assert.IsTrue(repo.IsSaveCalled); }
public void GetAll_WhenValid_ShouldReturnBugsCollection() { // Arrange var repo = new RepositoryMock<Bug>(); var bugs = new List<Bug>() { new Bug() { Text = "Test bug #1" }, new Bug() { Text = "Test bug #2" }, new Bug() { Text = "Test bug #3" } }; repo.Entities = bugs; var controller = new BugsController(repo); // Act var result = controller.GetAll(); // Assert CollectionAssert.AreEquivalent(bugs, result.ToList<Bug>()); }
public void PostBug_WhenBugTextIsInvalid_ShouldReturnBadRequest() { // Arrange var repo = new RepositoryMock<Bug>(); var controller = new BugsController(repo); this.SetupController(controller, "bugs"); // Act var newBug = new Bug() { Text = null }; var result = controller.PostBug(newBug).ExecuteAsync(new CancellationToken()).Result; // Assert Assert.AreEqual(HttpStatusCode.BadRequest, result.StatusCode); }
public void PostBug_WhenValid_ShouldAddBugToDb_WithMoq() { // Arrange var repoMock = new Mock<IRepository<Bug>>(); List<Bug> bugs = new List<Bug>() { new Bug() { Text = "Test bug #1" }, new Bug() { Text = "Test bug #2" } }; repoMock.Setup(repo => repo.Add(It.IsAny<Bug>())) .Callback((Bug b) => bugs.Add(b)); var controller = new BugsController(repoMock.Object); this.SetupController(controller, "bugs"); // Act var newBug = new Bug() { Text = "new bug" }; var result = controller.PostBug(newBug).ExecuteAsync(new CancellationToken()).Result; // Assert Assert.AreEqual(HttpStatusCode.Created, result.StatusCode); Assert.AreEqual(newBug.Text, bugs.Last().Text); Assert.AreEqual(BugStatus.New, bugs.Last().Status); Assert.IsNotNull(bugs.Last().DateCreated); }
public void GetAll_WhenValid_ShouldReturnBugsCollection_WithMoq() { // Arrange var repoMock = new Mock<IRepository<Bug>>(); Bug[] bugs = { new Bug() { Text = "Test bug #1" }, new Bug() { Text = "Test bug #2" } }; repoMock.Setup(repo => repo.All()) .Returns(bugs.AsQueryable()); var controller = new BugsController(repoMock.Object); // Act var result = controller.GetAll(); // Assert CollectionAssert.AreEquivalent(bugs, result.ToArray<Bug>()); }