Unit testing class in a web service in .net
Posted
by Dan Bailiff
on Stack Overflow
See other posts from Stack Overflow
or by Dan Bailiff
Published on 2010-05-18T00:09:56Z
Indexed on
2010/05/18
6:50 UTC
Read the original article
Hit count: 223
After some digging here, I took the advice in this thread: http://stackoverflow.com/questions/371961/how-to-unit-test-c-web-service-with-visual-studio-2008
I've created a separate class and my web service class is just a wrapper for that one. The problem is that when I try to create a unit test project in VS2008, it insists on creating a unit test that acts like I'm testing the web service calls instead of the class I specified. I can't get to the class I'm trying to test.
I have a web service "subscription_api.asmx". The code behind is "subscription_api.cs" which contains the web method wrapper calls to the real code at "subscription.cs".
I would expect to be able to do the following:
[TestMethod()]
public void GetSystemStatusTest()
{
subscription sub = new subscription();
XmlNode node = sub.GetSystemStatusTest();
Assert.IsNotNull(node);
}
But instead I get this mess which is autogenerated from VS'08:
/// <summary>
///A test for GetSystemStatus
///</summary>
// 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:\\CVSROOT\\rnr\\pro\\product\\wms\\ss\\subscription_api", "/subscription_api")]
[UrlToTest("http://localhost/subscription_api")]
public void GetSystemStatusTest()
{
subscription_Accessor target = new subscription_Accessor(); // TODO: Initialize to an appropriate value
XmlNode expected = null; // TODO: Initialize to an appropriate value
XmlNode actual;
actual = target.GetSystemStatus();
Assert.AreEqual(expected, actual);
Assert.Inconclusive("Verify the correctness of this test method.");
}
Additionally, there is a "subscription_api.accessor" in the Test References folder.
When I try this:
[TestMethod()]
public void GetSystemStatusTest2()
{
subscription_Accessor sub = new subscription_Accessor();
XmlNode node = sub.GetSystemStatus();
Assert.IsNotNull(node);
}
I get an error:
Test method subscription_api.Test.subscriptionTest.GetSystemStatusTest2 threw exception: System.TypeInitializationException: The type initializer for 'subscription_Accessor' threw an exception. ---> System.ArgumentNullException: Value cannot be null.
I'm really new to unit testing and feel lost. How can I create a unit test just for my subscription class in "subscription.cs" without testing the web service? Am I limited to testing within the same project (I hope not)? Do I have to put the target class in its own project outside of the web service project?
© Stack Overflow or respective owner