Integration testing between web application and WebAPI - c#

I am using WebApplicationFactory<TStartup> for integration testing of a .NET Core 3.0 web application.
It works when it need to test just one web application.
But what if web application need to send request to WebAPI application. I need somehow to instantiate both WebApplicationFactory<Web.Startup> and WebApplicationFactory<Api.Startup>.
I tried different alternatives with no luck.
Can anybody with experience of similar task point a right way to me.
P.S. Inside web application there is injected internal HttpClient that used internally in Controllers to send requests to API:
services.AddHttpClient<IInternalApiClient, InternalApiClient>();

You wouldn't. What you're talking about would be a "systems test", i.e. testing the whole system. Integration testing is about making sure the sub-components of a single application function together. As such, you'd mock the the service class that calls out to your API: InternalApiClient. In your test server's web host setup, you'd create a mock of IInternalApiClient and stub the appropriate methods to return dummy data instead of actually calling out to the API.

Related

Is it possible to call ASP.NET MVC controller methods from a test in a running MVC web application?

I'm learning ASP.Net MVC and I've read that it is easy to unit test as controllers can be instantiated and have their methods called without needing to deploy to a web server, and that it is easy to mock dependent objects etc. (see http://msdn.microsoft.com/en-us/library/gg416511(VS.98).aspx and multiple other references).
It's also possible to run acceptance level tests on the MVC App through the browser using Selenium or Waitin using your own favourite framework to author and control the tests.
What I'd like to know is if it's possible to run ATDD tests at an acceptance test level by calling the controller methods in a deployed, running MVC application, e.g. from Specflow, rather than having to test through the UI?
Or more generally, is it possible to programatically call the API of the ASP.Net MVC application from a test?
It seems like there is a gap in what it is possible to test between unit testing and browser based UI testing. Has anyone been able to bridge that gap?
Your question isn't clear but if you are asking what I think you are:
MVC controllers (generally) obey REST.
You can call any action on a controller (via a web server like IIS) by sending it a HTTP request. It will return a HTTP response containing the relevant data and data type(HTML, json, XML, etc.). So yes you can programatically call a controller like an API (if by API you mean something you send HTTP requests to and get HTTP responses from) therefore yes you can test the controller without using the UI.

Testing ASP.NET Web API

I am exposing a service using asp.net web API. each get/put/post calls internally corresponding WCF method after doing some logic like converting passed objects.
(using apicontroller, not odatacontroller)
I would like to test the written 'logic' but I can't make the WCF call yet as it is being written by some other team (for which I will write stub). I'm planning to write a test hook in each get/put.
My requirement is to write simple client code (not using paid tool, but a simple .exe which can test a running service) to test the logic written.
How do I go about it?
If you want to test Web API i suggest using this neat tool called RestSharp, its free not paid
Here is the URL : http://restsharp.org/
You can get it through Nuget Package Manager.
Once you install it, you would be able to test your service pretty easily.
It has examples to get you started on the first page.

Testing web service using C#

We need to test a web service which accepts JSON request. Server is implemented with WCF and REST API . Server response is asynchronous. I am thinking of below approach:
Approach 1: Use System.web and call UploadStringAsync
Approach 2: User asynchronous delegates in c#
I need to match response from server with request sent.
Is there any other better way to test REST API.
I need to develop test framework to test web service.
One part of testing a web service is testing what the web service does. You can do that with standard unit testing frameworks. This sort of testing has nothing to do with JSON or HTTP or REST, and that's a good thing. It will ensure that your service will work once you give it the correct JSON or HTTP or REST.
After you have tested the functionality of the service, you may want to write some other tests to make sure that the JSON, HTTP and REST part works. For that, you might use WebRequest or WebClient, or Visual Studio Web Tests.

Is it possible to use a c# application to emulate a browser?

I am trying to test a web service.
My first try was using unit tests which got really complicated because of the need to mock things like HttpContext (that actually was the main reason).
So now I'm trying a different angle - I know I can send a httpRequest using a c# application, but can this application for example maintain the cookies I'll receive from the web service?
Is it a possible and reasonable way to test a web service?
Thank you.
The logic 'hidden' behind the web service should be testable as unit-tests. The fact the results are returned across the wire is more of an integration test. Maintaining cookies across calls seems like an integration test to me.
BTW, designing stateless web service calls is desireable where possible.
Yes, That's possible for every practical use. As for the cookies example, see here.

web service testing without IIS

What is the best way to test web service using NUnit.
I want it to be automated, i.e, I don't want to have a separate process to host the web service for the testing code to consume.
The web service is just a plain-old-.net class. You can instantiate it directly and call its methods in a unit test.
That won't allow you to test http specific aspects of web services like authentication at the protocol level, but I would say that there's no getting around using a web server for that.
It depends on what you want to test. It's possible to do full integration tests and there is some value in that (checking serialization, for instance). One simple way to get good test coverage with minimal work is as follows:
Write one or more plain old classes that do the real work (use TDD if desired)
Test these classes in isolation
Have your WebMethods delegate to these classes.
Depends. If it's an asmx, you can use HostableWebCore on Vista and higher. If it's WCF, just self-host by creating an instance of ServiceHost in your process. You could directly instantiate the service impl, but if you have any HTTP-isms (HttpContext, Request/Reponse access, cookies, etc), you'll have to mock them.

Categories

Resources