Reputation: 4283
I'm very new to nUnit, testing in general, and this is my first test method. I wish I did TDD, but it's too late and I have to implement Unit testing on existing code.
I'm getting this error: HttpContext is not available. This class can only be used in the context of an ASP.NET request.
Every other methods that don't reference the service passes the test fine. How do I fix it?
namespace MyWCFServiceTests
{
[TestFixture]
public class Class1
{
[Test]
public void myMethod()
{
MyWCFService.Service1 wcf = new MyWCFService.Service1();
wcf.MyMethod();
}
}
}
Upvotes: 1
Views: 2823
Reputation: 15159
Looks like your service requires ASP.NET hosting. If that is the case it depends on HttpContext and you won't be able to mock it. It's worth mentioning this is not unit testing but rather functional/integration because you test whole WCF pipeline (serialization, network stack, hosting environment, probably storage layer, etc.). You have the following options though:
Host the service on IIS (like you probably do in production) and make you tests just regular WCF clients
Try to convert the service so that it can be self hosted if possible
Refactor service implementation so that you can (unit-) test the logic without WCF (you'll end up with lot of dependencies but that is a good thing)
Upvotes: 0
Reputation: 1140
You should consider using one of the Mocking frameworks out there (Moq, TypeMock etc.) Here is an article that explains the basic concepts of mocking WCF services with Moq
Upvotes: 1