views:

497

answers:

1

How do I write unit tests to test the Web methods of a Web service using NUnit?

The web methods in this application will add,update and delete a record in the database. The unit test will test a web method whether a record has been inserted in the database, the webmethod calls a method in data access layer to perform this action.

+1  A: 

I do not think it's appropriate to be testing the end result of your web service with a unit test. Also, what you are trying to do is called an "integration test", and not a unit test.

What you can do, however, is to:

  • Write unit tests to check if your data access layer (DAL) is working properly
  • Write unit tests to see if your web method is properly accessing your DAL

You might also want to look at a question I raised before: How do I unit test persistence? to provide you more insight.

If you really are adamant to be able to do this however, it is possible to create such unit tests using MbUnit, which has the Rollback attribute.

[Rollback]
public void Test_database_persistence()
{
    //any database access you perform here will be put inside a transaction 
    //and rolled back afterwards
}

MbUnit is totally compatible with NUnit, so you could still use tests you've already written with NUnit.

Jon Limjap