public void AddResultWithValidDbReturnsOk() { //Setup var mockEventsRepo = new Mock <IEventRepo>(); var mockEventResultsRepo = new Mock <IEventResultsRepo>(); var mockUserRepo = new Mock <IUserRepo>(); var result = new AddResultModel() { EventId = 1, Hundreths = 100, Minutes = 20, Seconds = 30, UserId = "a1" }; mockEventResultsRepo.Setup(mERR => mERR.AddEventResult(result)).Returns(true); var sut = new EventsController(mockEventsRepo.Object, mockEventResultsRepo.Object, mockUserRepo.Object); //Action var res = sut.AddResult(result); //Assert res.Should().BeOfType <OkResult>(); }
//POST api/Events/AddResult public IHttpActionResult AddResult(AddResultModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var res = _eventResultsRepo.AddEventResult(model); return(res ? (IHttpActionResult)Ok() : BadRequest("Unable To add to database")); }
public void AddResultWithInvalidModelReturnsBadRequest() { //Setup var mockEventsRepo = new Mock <IEventRepo>(); var mockEventResultsRepo = new Mock <IEventResultsRepo>(); var mockUserRepo = new Mock <IUserRepo>(); var result = new AddResultModel(); var sut = new EventsController(mockEventsRepo.Object, mockEventResultsRepo.Object, mockUserRepo.Object); sut.ModelState.AddModelError("EventId", "EventId is required"); //Action var res = sut.AddResult(result); //Assert res.Should().BeOfType <InvalidModelStateResult>(); res.As <InvalidModelStateResult>().ModelState.IsValid.Should().BeFalse(); }