Unit Test WCF using ChannelFactory - wcf

I am working on some WCF services and they will be deployed to Azure, so I would like to try to accomplish as many unit tests as I can locally, in order to avoid useless roundtrips to the staging area while we run the integration tests.
The services use the CQRS pattern and I have layered everything in the following way:
- A bootstrapper to inject dependency into the WCF services
- A command for each service method
- A command handler to intercept the commands
So far I can fully unit test the commands, the handlers and the bootstrapper. What I still need to write is this type of test:
[TestMethod]
public void Client_ReceiveValidCommand_WillExecute()
{
var factory = new ChannelFactory<IDomainWriteService>("*");
var service = factory.CreateChannel();
var expectedCommand = BuildAddUsersCommand();
service.AddUsers(expectedCommand);
}
The problem is that this test pretends a valid endpoint in the configuration file.
Is there any way that I can start something like IIS express while my test fitness starts and then plug the channel factory into this instance of IIS?
I simply can't create a staging area where I can deploy my services and run my integration tests so I would prefer to follow the unit test way where possible.

I found an interesting article here: http://www.reimers.dk/jacob-reimers-blog/testing-your-web-application-with-iis-express-and-unit-tests which explains how to host your test in IIS express and of course run IIS express within your unit tests.
In this way I can simply create on fly a new instance of IIS express and host my WCF service, run the unit tests and finally destroy everything.

Related

RestAssured testing without running Tomcat

I have REST web service which needs to be tested. I am using Mockito for mocking DAO classes and RestAssured for testing REST methods through URI. Is there any way to test REST service without running it separately with Tomcat? Or how to run application on Tomcat with mocked classes before test cases?
There is a tutorial that shows you how to use maven to start an embedded instance of tomcat and run tests against your service using RestAssured:
http://www.hascode.com/2011/09/rest-assured-vs-jersey-test-framework-testing-your-restful-web-services/
You start tomcat in one shell and run your tests in another.
However, I strongly recommend using the jersey test framework which transparently spins up an embedded container. In this case you wouldn't use RestAssured at all, but the jersey test client. Your tests will run more quickly and with less fuss. It's well documented here: https://jersey.github.io/documentation/latest/test-framework.html. The tutorial also demonstrates this approach, though it doesn't seem to me that the client is correctly constructed.
In the past I've also tested REST resources by calling the implementing class methods directly. Though this doesn't test the correct mapping of the http query parameters/body to java method parameters, it was often sufficient (especially when I'm also coding the client side code).

Can we have a Unit test project dedicated only to WCF in Visual Studios 2013?

Just wanted to know whether we can have a Unit Test Project dedicated to a WCF Solution or not ? I know this might be a Generic question but I couldn't get specific answer to it even though I searched through various blogs and websites. Please suggest.
yes its possible to unit test WCF services. The options are:
create classes which do all of the work which are separate from the WCF service. Unit test these classes in isolation. Make your service a thin wrapper which calls these classes.
test directly with the WCF service class without it being hosted (a bit like this example)
Have the unit tests host the WCF service in process and then call the services via named pipes or similar (see the console example here for details of how to self-host). When you self host you should start the service in the test setup or the test fixture setup and then destroy it in the corresponding teardown.

Load test Application + WCF service

I want to load test an application having its own WCF service using Visual Studio 2012. I tried creating a web performance test and use it with load test but, web performance is not recording the intermediate request send to service which is very much required.
Using CodedUI with it not feasible since it does not put on that much load and and its interactive.
The application is on different server and service too.
Any suggestion to accomplish this in VS2012.
Use dedicated "Unit Tests" for this. VS Load Tests can execute Coded UI/Web Performance or Unit Test.
In your Unit Tests, simply use a standard WCF proxy, channelfactory or RestClient.
Just check that you dispose well a Web Service Proxy class

Unit Testing WCF Visual Studio 2010

Im writting unit test for a WCF service which i have written. The thing is when i run the Unit test it throw me error saying that there is no end point listening. I need to compile my WCF Service and later run the unit test.
Is there any other way where Unit Test runs and host the WCF service and later start testing.
Thanks in advance
Running a test against an actual WCF service would not be unit testing, it would be integration testing; if you want to do this you'd do it as part of an automated build process which deploys the service and then runs your tests.
To unit test your service, you should ignore the WCF aspect and call the service class directly, as if it were any other class.

How do I host more than one WCF Sequential Workflow in WCF Workflow Library

I have two workflows in my sequential workflow service library project. But i cannot seem to configure both of the services to run in my wcf service host app. When the service host loads the one service has started but the other is stopped. No amount of fiddling with the app.config file seems to make any difference. How would one configure the hostto support running two WCF Workflows at the same time using the app.config file?
This seems like it would to be easy to configure... just like when you have 2 services running in the same wcf host. Any ideas?
The simple answer is you can't, if I understand your question.
Each WorkflowServiceHost can host one endpoint with one workflow as the implementation of the service contract.
You can configure them in different WorkflowServiceHosts with different endpoint configurations, though.
WorkflowServiceHost host1 = new WorkflowServiceHost(typeof(MyFirstWorkflow), new Uri("URL1"));
WorkflowServiceHost host2 = new WorkflowServiceHost(typeof(MySecondWorkflow), new Uri("URL2"));
host1.Open();
host2.Open();
Hopefully this helps.