using System.Web.Http;
namespace Controllers
{
public class ValuesController : ApiController
{
// GET api/values/5
public string Get(int id)
{
return "value";
}
}
}
Tworzymy klasę Startup
using Owin;
using System.Web.Http;
namespace OwinSelfhostSample
{
public class Startup
{
public void Configuration(IAppBuilder appBuilder)
{
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
appBuilder.UseWebApi(config);
config.MapHttpAttributeRoutes();
config.EnsureInitialized();
}
}
}
Tworzymy test
[TestClass]
public class UnitTest
{
[TestMethod]
public async Task TestMethod()
{
// Arrange
var baseAddress = "https://localhost:44365/";
var endpoint = "api/values/1";
WebApp.Start<Startup>(url: baseAddress);
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(baseAddress);
// Act
var response = await client.GetAsync(endpoint);
// Assert
Assert.IsTrue(response.IsSuccessStatusCode);
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}
}
W .NET Framework nie ma takiej paczki. Aby przeprowadzić testy integracyjne na podobnej zasadzie musimy dostosować nasz projekt do self-hostingu.
Dodajemy paczkę
Install-Package Microsoft.AspNet.WebApi.OwinSelfHost
Tworzymy przykładowy kontroler