we have a azure mobile service implemented for one cross platform mobile app. We have used azure mobile services to expose the back end to mobile apps.
All the controllers we are using extending table controller and most of the service methods has some custom logic implemented. Now we wanted to unit test these custom logic.
Is there any way we can mock table controllers and write unit tests? I have tried everywhere and even i have checked MSDN forums also. I could n't get any info about it. Let me know, if anyone done this and can share a sample.
All the interactions with the backend database are done through the DomainManager, so you are likely to have to moch the EntityDomainManager that you use in your TableControllers when testing. The SDK has a ctor overload for TableController that takes a DomainManager. You could register your mock with Autofac in your tests and have that get used during testing (if you are testing the full pipeline). If you are hitting the TableController in isolation with new YourController(), you can just use that overload to pass in your mocked DomainMAnager and make sure you don't create a real DomainManager in Initialize.
Related
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.
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.
I'm designing an enterprise level web app that will have a Data layer + SQL database, a WCF REST web service layer that communicates with the data layer, then an MVC website which will communicate to the WCF REST web services. I want to have automatic unit testing at each level of the design.
I have created a data layer unit test project fine, which tests the EF models, repositories, operations etc and have about 15 tests I can click a button for and run which so far, tests all the functionality the data layer provides.
I want to have the same at the WCF service layer but I cannot figure out how to have an automatic test project to run against a WCF service. I've seen articles on using WCF test client to load the service in the background and be able to input parameters to each service method and see results, but I don't want a manual process, I want it to be automated, to mock the data layer and test the service calls in isolation. Complicated by the fact that ideally I would like to test the RESTful side of it, so being able to simulate GET/POST/PUT/DELETE etc and also to call certain methods with the wrong method and to confirm it fails as expected and so forth.
Is there a good solution to do this automatically or am I approaching this wrong?
You sure can create tests that can automatically run to test WCF services.
See SO discussion here:WCF Unit Test
However one question you need to ask in your test strategy / test case design is that howmuch of the REST endpoint testing need to be unit tests and how much need to be part of system/ integration test. Because the wcf endpoints (including REST) will allow you to treat the system as a black box and do system tests without having any intimate knowledge of the internal types.
I wrote an article on how to do that. You have to abstract the business logic to a service which either calls the real service or a mocked one. http://gaui.is/how-to-mock-and-test-wcf-services-using-moq/
you can change the address of the service to localhost.
this way you can start the service and test it with no mocking
I have a VS 2010 solution that contains two projects: the first is a WCF service, and the second is a unit testing project, holding a reference to the service and testing the methods the service is exposing. The unit test project is developed using the Microsoft.VisualStudio.TestTools.UnitTesting framework.
Before running the testing project I run locally the WCF service using right clock on the SVC file and choose the option "View in Browser". Only when the service is "up in the air" the tests can actually run.
My question is whether there is an option to run the service automatically before the unit tests start running, using a C# code or some kind of a script?
If you truly want to operate on hosted service, you can self-host it within unit test. Then it will be independent from infrastructure, making it repeatable, so future developers will have ability to run them without making any additional setups.
But think twice, do you want to test service logic or its hosting infrastructure? WCF has decoupled implementation by using contracts (interfaces). You can successfully test service logic by testing service class as any other class, calling web service methods as any other methods.
If your service has an external dependencies (like other services), they should be maintained by Dependency Injection. It will allow you to mock them. If you service relies on global, or static resources (like OperationContext), create a wrapper, that will allow you to inject its mock also. Or use more sophisticated solution like Microsoft Fakes (refer also this tutorial) or Typemock Isolator.
When you will have all logic tested, then you can proceed with integration test with IIS hosted service, testing connectivity to other resources and boundary conditions like timeouts etc.
Install or configure IIS and run the WCF service from a URL on localhost
http://localhost/my.wcf.service
Run your tests from this.
Although, in the strictest sense of the word, running tests on your code via http is an integration test and not a unit test.
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.