public void CreateReturnsNewlyCreatedIdeaForSession() { var mockRepo = new Mock <IBrainstormSessionRepository>(); int testSessionId = 123; string testName = "test name"; string testDescription = "test description"; var testSession = GetTestSession(); mockRepo.Setup(r => r.GetById(testSessionId)).Returns(testSession); var controller = new IdeasController(mockRepo.Object); var newIdea = new NewIdeaModel() { Description = testDescription, Name = testName, SessionId = testSessionId }; mockRepo.Setup(r => r.Update(testSession)).Verifiable(); var result = Assert.IsType <HttpOkObjectResult>(controller.Create(newIdea)); var returnSession = Assert.IsType <BrainstormSession>(result.Value); mockRepo.Verify(); Assert.Equal(2, returnSession.Ideas.Count()); Assert.Equal(testName, returnSession.Ideas.LastOrDefault().Name); Assert.Equal(testDescription, returnSession.Ideas.LastOrDefault().Description); }
public async Task CreatePersistsNewIdea() { var idea = new Idea { Name = "New idea", Description = "Persist an idea", Rating = 1 }; await _controller.Create(idea); // Fetch the ideas back out of the database var ideas = await _context.Ideas.ToListAsync(); Assert.That(ideas, Has.Count.EqualTo(NUM_IDEAS + 1)); Assert.That(ideas, Has.One.EqualTo(idea).Using(new IdeaEqualityComparer())); }
public void CreateReturnsBadRequestGivenInvalidModel() { var mockRepo = new Mock <IBrainstormSessionRepository>(); var controller = new IdeasController(mockRepo.Object); controller.ModelState.AddModelError("error", "some error"); var result = Assert.IsType <BadRequestObjectResult>(controller.Create(null)); }
public void CreateReturnsHttpNotFoundForInvalidSession() { var mockRepo = new Mock <IBrainstormSessionRepository>(); int testSessionId = 123; mockRepo.Setup(r => r.GetById(testSessionId)).Returns((BrainstormSession)null); var controller = new IdeasController(mockRepo.Object); var result = Assert.IsType <HttpNotFoundObjectResult>(controller.Create(new NewIdeaModel())); }
public async Task Create_ResturnsBadRequest_GivenInvalidMidel() { var mockRepo = new Mock <IBrainstormSessionRepository>(); var controller = new IdeasController(mockRepo.Object); controller.ModelState.AddModelError("error", "some error"); var result = await controller.Create(model : null); Assert.IsType <BadRequestObjectResult>(result); }
public async Task Create_ReturnHttpNotFound_ForInvalidSession() { int testSessionId = 123; var mockRepo = new Mock <IBrainstormSessionRepository>(); mockRepo.Setup(x => x.GetByIdAsync(testSessionId)) .ReturnsAsync((BrainstormSession)null); var controller = new IdeasController(mockRepo.Object); var result = await controller.Create(new NewIdeaModel()); Assert.IsType <NotFoundObjectResult>(result); }
public async Task Create_ReturnsBadRequest_GivenInvalidModel() { // Arrange & Act var mockRepo = new Mock <IBrainstormSessionRepository>(); var controller = new IdeasController(mockRepo.Object); controller.ModelState.AddModelError("error", "some error"); // Act var result = await controller.Create(model : null); // Assert Assert.That(result, Is.TypeOf <BadRequestObjectResult>()); }
public async Task Create_ReturnsBadRequest_GivenInvalidModel() { // Arrange & Act var mockRepo = new Mock <IBrainstormSessionRepository>(); var controller = new IdeasController(mockRepo.Object); controller.ModelState.AddModelError("error", "some error"); // Act var result = await controller.Create(model : null); // Assert //Assert.IsType<BadRequestObjectResult>(result);//xUnit Assert methode moet vervangen worden door gelijke Assert in NUnit: Assert.IsInstanceOf <BadRequestObjectResult>(result); }
public async Task Create_ReturnsHttpNotFound_ForInvalidSession() { // Arrange int testSessionId = 123; var mockRepo = new Mock <IBrainstormSessionRepository>(); mockRepo.Setup(repo => repo.GetByIdAsync(testSessionId)) .ReturnsAsync((BrainstormSession)null); var controller = new IdeasController(mockRepo.Object); // Act var result = await controller.Create(new NewIdeaModel()); // Assert Assert.That(result, Is.TypeOf <NotFoundObjectResult>()); }
public async Task Create_ReturnsBadRequest_GivenInvalidModel() { // Arrange & Act var fixture = new Fixture(); var newIdea = fixture.Create <NewIdeaModel>(); var validatorMock = new Mock <IValidatorStrategy <NewIdeaModel> >(); validatorMock.Setup(x => x.IsValid(newIdea)).Returns(false); var mockRepo = new Mock <IBrainstormSessionRepository>(); var controller = new IdeasController(mockRepo.Object, validatorMock.Object); // controller.ModelState.AddModelError("error", "some error"); var model = fixture.Build <NewIdeaModel>().Without(x => x.Name).Without(x => x.Description).Create(); // Act var result = await controller.Create(model); // Assert Assert.IsType <BadRequestObjectResult>(result); }
public async Task Create_ReturnsNewlyCreatedIdeaForSession() { // Arrange int testSessionId = 123; string testName = "test name"; string testDescription = "test description"; var testSession = GetTestSession(); var mockRepo = new Mock <IBrainstormSessionRepository>(); mockRepo.Setup(repo => repo.GetByIdAsync(testSessionId)) .ReturnsAsync(testSession); var controller = new IdeasController(mockRepo.Object); var newIdea = new NewIdeaModel() { Description = testDescription, Name = testName, SessionId = testSessionId }; mockRepo.Setup(repo => repo.UpdateAsync(testSession)) .Returns(Task.CompletedTask) .Verifiable(); // Act var result = await controller.Create(newIdea); // Assert //var okResult = Assert.IsType<OkObjectResult>(result); xUnit Assert.That(result, Is.TypeOf <OkObjectResult>()); //of Assert.IsInstanceOf<OkObjectResult>(result); Nunit var okResult = result as OkObjectResult; //var returnSession = Assert.IsType<BrainstormSession>(okResult.Value); xUnit Assert.That(okResult.Value, Is.TypeOf <BrainstormSession>());//of Assert.IsInstanceOf<.... var returnSession = okResult.Value as BrainstormSession; mockRepo.Verify(); Assert.AreEqual(2, returnSession.Ideas.Count()); Assert.AreEqual(testName, returnSession.Ideas.LastOrDefault().Name); Assert.AreEqual(testDescription, returnSession.Ideas.LastOrDefault().Description); }