public void Post_ShouldCreateNewEntityAndReturnSuccess() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- var releaseToCreate = new NewsReleaseViewModel { Key = TestData.TestNewsRelease.Key, PublishDateTime = DateTime.Now }; var mockRepository = CreateDataStore(); mockRepository.Setup(r => r.AddEntity(It.IsAny <NewsRelease>())).Verifiable(); var controller = new NewsReleasesController(mockRepository.Object, _logger.Object, _mapper.Object); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- var result = controller.Post(releaseToCreate) as StatusCodeResult; //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- result.Should().BeOfType(typeof(StatusCodeResult), "because the create operation should go smoothly"); result.StatusCode.Should().Be(201, "because HTTP Status 201 should be returned upon creation of new entity"); // this will throw if the System-Under-Test (SUT) i.e. the controller didn't call repository.AddEntity(...) mockRepository.Verify(); }
public void GetById_ShouldReturnNotFound_WhenGivenInvalidId() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- var mockRepository = CreateDataStore(); var controller = new NewsReleasesController(mockRepository.Object, _logger.Object, _mapper.Object); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- var result = controller.GetById("-1"); // does not exist... //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- result.Should().BeOfType(typeof(NotFoundResult), "because an invalid Id should not yield a result"); }
public void Delete_ShouldReturnNotFound_WhenGivenInvalidId() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- var mockRepository = CreateDataStore(); var controller = new NewsReleasesController(mockRepository.Object, _logger.Object, _mapper.Object); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- var result = controller.Delete("-1"); // does not exist... //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- result.Should().BeOfType(typeof(NotFoundObjectResult), "because a valid Id is required to delete an entity"); }
public void Put_ShouldUpdateEntityAndReturnSuccess() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- var mockRepository = CreateDataStore(); mockRepository.Setup(r => r.Update("0", It.IsAny <NewsRelease>())).Returns(_expectedModelReturn); var controller = new NewsReleasesController(mockRepository.Object, _logger.Object, _mapper.Object); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- var result = controller.Put("0", _mapper.Object.Map <NewsRelease, NewsReleaseViewModel>(_expectedModelReturn)); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- result.Should().BeOfType(typeof(OkObjectResult), "because the update operation should go smoothly"); }
public void Delete_ShouldReturnBadRequest_WhenDataSourceIsUnavailable() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- var mockRepository = CreateDataStore(); mockRepository.Setup(r => r.GetReleaseByKey(It.IsAny <string>())).Throws <InvalidOperationException>(); var controller = new NewsReleasesController(mockRepository.Object, _logger.Object, _mapper.Object); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- var result = controller.Delete("0") as BadRequestObjectResult; //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- result.Should().BeOfType(typeof(BadRequestObjectResult), "because the delete operation should require a valid data source"); result.StatusCode.Should().Be(400, "because HTTP Status 400 should be returned to signal a Bad Request"); }
public void Post_ShouldReturnBadRequest_WhenGivenInvalidModel() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- var mockRepository = CreateDataStore(); var controller = new NewsReleasesController(mockRepository.Object, _logger.Object, _mapper.Object); controller.ModelState.AddModelError("error", "some validation error"); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- var result = controller.Post(model: null) as BadRequestObjectResult; //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- result.Should().BeOfType(typeof(BadRequestObjectResult), "because the create operation should not work with invalid data"); result.StatusCode.Should().Be(400, "because HTTP Status 400 should be returned to signal a Bad Request"); }
public void GetAllNewsReleases_ShouldReturnSuccess() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- var paginationParams = new NewsReleaseParams(); var expectedReleasesPerPage = paginationParams.PageSize; var mockRepository = CreateDataStore(); var controller = new NewsReleasesController(mockRepository.Object, _logger.Object, _mapper.Object); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- var results = controller.GetResultsPage(paginationParams); var actualNumberOfReleases = results.Count(); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- actualNumberOfReleases.Should().Be(expectedReleasesPerPage); }
public void Delete_ShouldDeleteEntityAndReturnSuccess() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- var mockRepository = CreateDataStore(); mockRepository.Setup(r => r.Delete(It.IsAny <NewsRelease>())).Verifiable(); var controller = new NewsReleasesController(mockRepository.Object, _logger.Object, _mapper.Object); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- var result = controller.Delete("0"); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- result.Should().BeOfType(typeof(OkResult), "because the delete operation should go smoothly"); // this will throw if the System-Under-Test (SUT) i.e. the controller didn't call repository.Delete(...) mockRepository.Verify(); }
public void GetById_ShouldReturnSuccess() { //----------------------------------------------------------------------------------------------------------- // Arrange //----------------------------------------------------------------------------------------------------------- var mockRepository = CreateDataStore(); var controller = new NewsReleasesController(mockRepository.Object, _logger.Object, _mapper.Object); //----------------------------------------------------------------------------------------------------------- // Act //----------------------------------------------------------------------------------------------------------- var result = controller.GetById("0"); //----------------------------------------------------------------------------------------------------------- // Assert //----------------------------------------------------------------------------------------------------------- result.Should().BeOfType(typeof(OkObjectResult), "because the read operation should go smoothly"); var actualValue = ((result as OkObjectResult).Value as NewsReleaseViewModel); actualValue.Key.Should().Be(_expectedModelReturn.Key); actualValue.PublishDateTime.Should().Be(_expectedModelReturn.PublishDateTime); actualValue.Keywords.Should().Be(_expectedModelReturn.Keywords); }