المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : How to test Web Methods with NUnit



C# Programming
08-05-2009, 03:30 PM
Hi all,

In my solution I already have lots of business object unit tests. However I also want to test the web service layer. So to do this I've added another project to the solution and added a project reference to the web service layer. So now I can see all the web methods.

The problem I have is that when I want to go to the database my method GetDAOFactory() always returns null. I think this is because the global file inherits the HttpApplication class. E.G.

Try and get the customer or create a new one
1)

private Customer GetCustomerInSession()
{
Customer customer = Session[PropertyCustomer] as Customer;
if (customer == null)
{
Session[PropertyCustomer] = customer = new Customer(GetDAOFactory());
}
return customer;
}

Get the DAO Factory this returns null
2)

private IDAOFactory GetDAOFactory()
{
return Application[Global.DAOFactorySetting] as IDAOFactory;
}

I think the reason is because global inherits HttpApplication
3)

public class Global : HttpApplication
{

protected void Application_Start(object sender, EventArgs e)
{
// create data access implementation
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DevBox"].ConnectionString;
Application[DAOFactorySetting] = new ADODAOFactory(ProviderType.SqlServer, connectionString);
}

protected void Application_End(object sender, EventArgs e)
{

}

public static readonly string DAOFactorySetting = "DAOFactory";
}

Any ideas why its returning null?

I've thought of one solution and that would be to change the reference to a web reference. The only problem then is that I would either have to add a localhost web reference which wouldn't work with the CI build. Or to reference the published development app layer, but that would mean that when I change my code I wouldn't be able to test it until it was committed!!

Any help would be greatly appreciated. I'm using C#.Net 2.0 with NUnit 2.4.7

Phil

P.S. I've copied the connectionString DevBox to the App.config in the WebServiceTests project so that it can see it.