public void CreateInvalidLine() { var mocker = new RouteServiceMocker(); RouteCreateModel model = new RouteCreateModel(); mocker.RepoMock.Setup(r => r.Add(It.IsAny <RouteInfoEntity>())).Callback(() => Console.WriteLine("Unit test create")); mocker.RepoMock.Setup(r => r.IsValidLine(It.IsAny <string>())).Returns(() => false); mocker.UserResolverMock.Setup(r => r.IsConfirmed()).Returns(() => Task.FromResult <bool>(true)); IRouteBLService service = mocker.GetService(); var result = service.Create(new RouteCreateModel()); Assert.Equal(ErrorStatus.InvalidModel, result.Result.Error.Status); Assert.Equal("Invalid route line", result.Result.Error.Message); }
public void CreateUnhandledException() { var mocker = new RouteServiceMocker(); RouteInfoEntity entity = new RouteInfoEntity(); mocker.RepoMock.Setup(r => r.Add(It.IsAny <RouteInfoEntity>())).Callback(() => Console.WriteLine("Unit test create")); mocker.RepoMock.Setup(r => r.IsValidLine(It.IsAny <string>())).Returns(() => true); mocker.RepoMock.Setup(r => r.Commit()).Throws(new Exception("testexception")); mocker.UserResolverMock.Setup(r => r.IsConfirmed()).Returns(() => Task.FromResult <bool>(true)); IRouteBLService service = mocker.GetService(); var result = service.Create(new RouteCreateModel()); Assert.Equal(ErrorStatus.InternalServer, result.Result.Error.Status); Assert.Equal("testexception", result.Result.Error.Message); }
public async Task <IActionResult> Create([FromBody] RouteCreateModel model) { if (!ModelState.IsValid) { return(WrapResponse(new Result <RouteViewModel>(WrapModelStateErrors(ModelState)))); } var result = await _routeService.Create(model); if (result.IsOk) { var idUrl = Url.Action("GetById", new { id = result.Content }); Result <string> urlResult = new Result <string>(idUrl); return(WrapResponse(urlResult, HttpStatusCode.Created)); } return(WrapResponse(result, HttpStatusCode.Created)); }