NZJames
NZJames

Reputation: 5055

Unit testing a WCF service

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?

Upvotes: 4

Views: 30592

Answers (3)

Gaui
Gaui

Reputation: 8919

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/

Upvotes: 1

Mzf
Mzf

Reputation: 5260

you can change the address of the service to localhost. this way you can start the service and test it with no mocking

Upvotes: -1

George Philip
George Philip

Reputation: 704

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.

Upvotes: 3

Related Questions