2

I have web api controller and i want to perform an integration tests. So I followed the article here to configured the in-memory Web API host. My integration test and web api are two different projects in same VS solution.

Below is the code

Web API Controller

public class DocumentController : ApiController
{

    public DocumentController(IDomainService domainService)
    {
        _domainService = domainService;            
    }        

    [HttpPost]
    public async Task<IEnumerable<string>> Update([FromBody]IEnumerable<Document> request)
    {
        return await _domainService.Update(request).ConfigureAwait(false);
    }
}

Integration Test

    [TestClass]
    public class IntegrationTests
    {
        private HttpServer _server;
        private string _url = "http://www.strathweb.com/";

        [TestInitialize]
        public void Init()
        {
            var config = new HttpConfiguration();
            config.Routes.MapHttpRoute(name: "Default", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional });
            config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
            config.MessageHandlers.Add()
            _server = new HttpServer(config);
        }


        [TestMethod]
        public void UpdateTransformedDocuments()
        {
            var doc = new Document()
            {
              // set all properties
            }
            var client = new HttpClient(_server);
            var request = createRequest<Document>("api/document/Update", "application/json", HttpMethod.Post, doc, new JsonMediaTypeFormatter());

            using (var response = client.SendAsync(request).Result)
            {
                 // do something with response here
            }
        }        

        private HttpRequestMessage createRequest<T>(string url, string mthv, HttpMethod method, T content, MediaTypeFormatter formatter) where T : class
        {
            Create HttpRequestMessage here
        }
    }

However im getting error

StatusCode: 404, ReasonPhrase: 'Not Found'

How & where do I tell the HttpServer to execute DocumentController?

Update1 So I fixed above error by changing the [TestIntialize] code as below

    [TestInitialize]
    public void Init()
    {
        var config = new HttpConfiguration();
        UnityWebApiActivator.Start();
        WebApiConfig.Register(config);
        config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
        _server = new HttpServer(config);
    }

I don't get 404 error now. However Unity is not able to resolve DocumentController. The HttpResponse contains error

An error occurred when trying to create a controller of type 'DocumentController'. Make sure that the controller has a parameterless public constructor.

In TestInitialize method I'm calling UnityWebApiActivator.Start() which registers all the require types with Unity.

1 Answer 1

2

i resolved my 2nd issue by setting 'HttpConfiguration.DependencyResolver'

    [TestInitialize]
    public void Init()
    {
        var config = new HttpConfiguration();
        //UnityWebApiActivator.Start();
        config.DependencyResolver = new UnityHierarchicalDependencyResolver(UnityConfig.GetConfiguredContainer());
        WebApiConfig.Register(config);
        config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
        _server = new HttpServer(config);
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.