public async Task CreateAsync_ModelIsValid_Success() { const string name = "Alexey"; var model = new PostClientModel { Name = name }; // arrange ClientServiceMockRepository.Setup( service => service.CreateAsync<PostClientModel, ClientModel>(model)) .ReturnsAsync(new ClientModel { Name = name, Id = Guid.NewGuid().ToString() }); const string requestedUrl = "http://localhost/clients"; using (var config = new HttpConfiguration()) { config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); using (var request = new HttpRequestMessage(HttpMethod.Post, requestedUrl)) { request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, config); using (var controller = new ClientsController(MockApiServices, MockUnitOfWork, MockClientService, MockAccountService)) { controller.RequestContext.RouteData = new HttpRouteData(route: new HttpRoute(), values: new HttpRouteValueDictionary {{"controller", "clients"}}); controller.Request = request; controller.Configuration = config; using (HttpResponseMessage response = await controller.CreateAsync(model)) { Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); Assert.AreEqual(new Uri(requestedUrl), response.RequestMessage.RequestUri); ClientModel createdModel = await response.Content.ReadAsAsync<ClientModel>(); Assert.IsNotNull(createdModel); Assert.IsNotNull(createdModel.Name); Assert.AreEqual(name, createdModel.Name); } } } } }
public async Task CreateAsync_ModelIsNull_ReturnBadRequestCode() { const string requestedUrl = "http://localhost/clients"; using (var config = new HttpConfiguration()) { using (var request = new HttpRequestMessage(HttpMethod.Post, requestedUrl)) { request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, config); using (var controller = new ClientsController(MockApiServices, MockUnitOfWork, MockClientService, MockAccountService)) { controller.Request = request; controller.Configuration = config; using (HttpResponseMessage response = await controller.CreateAsync(null)) { Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode); } } } } }