The scenario:
- Visual Studio 2010, ASP.NET web application
- Create a web service class and give it some
WebMethod-attributed methods - Have Visual Studio auto-generate unit tests for the methods, by right-clicking in the class definition and choosing
Create Unit Tests... -
Note that the generated code for each test includes this boilerplate:
// TODO: Ensure that the UrlToTest attribute specifies a URL to an ASP.NET page // (for example, http://.../Default.aspx). This is necessary for the unit test to // be executed on the web server, whether you are testing a page, web service, or // a WCF service. [TestMethod] [HostType("ASP.NET")] [AspNetDevelopmentServerHost("C:\\...\\ProjectName", "/")] [UrlToTest("http://localhost:59733/")] public void MethodNameTest() -
Add in
Default.aspxtoUrlToTest, as requested by the comment:[UrlToTest("http://localhost:59733/Default.aspx")] -
Run all tests in the class
The problem:
Inconsistently, some tests fail with
The communication channel with ASP.NET could not be configured. Requested Service not found
Which tests fail and which tests pass can vary from run to run. There appears to be no pattern to the failures, but it’s never the case that all successfully run.
What’s going wrong?
Is it the case that the page you’ve specified in
UrlToTestalways performs aResponse.Redirecton load? Because if it is, this will the the cause of the failures you’re seeing.Change the URL specified in
UrlToTestto that of a page that does not perform aResponse.Redirect, and your tests should run fine.