开发者

How do I create a unit test that updates a record into database in asp.net

How do I create a unit test that updates a record into database in 开发者_开发问答asp.net


While technically we don't call this a 'unit test', but an 'integration test' (as Oded explained), you can do this by using a unit testing framework such as MSTest (part of Visual Studio 2008/2010 professional) or one of the free available unit testing frameworks, such as NUnit.

However, testing an ASP.NET web project is usually pretty hard, especially when you've put all you logic inside web pages. Best thing to do is to extract all your business logic to a separate layer (usually a separate project within your solution) and call that logic from within your web pages. But perhaps you’ve already got this separation, which would be great.

This way you can also call this logic from within your tests. For integration tests, it is best to have a separate test database. A test database must contain a known (and stable) set of data (or be completely empty). Do not use a copy of your production database, because when data changes, your tests might suddenly fail. Also you should make sure that all changes in the database, made by an integration test, should be rolled back. Otherwise, the data in your test database is constantly changing, which could cause your tests to suddenly fail.

I always use the TransactionScope in my integration tests (and never in my production code). This ensures that all data will be rolled back. Here is an example of what such an integration test might look like, while using MSTest:

[TestClass]
public class CustomerMovedCommandTests
{
    // This test test whether the Execute method of the
    // CustomerMovedCommand class in the business layer
    // does the expected changes in the database.
    [TestMethod]
    public void Execute_WithValidAddress_Succeeds()
    {
        using (new TransactionScope())
        {
            // Arrange
            int custId = 100;

            using (var db = new ContextFactory.CreateContext())
            {
                // Insert customer 100 into test database.
                db.Customers.InsertOnSubmit(new Customer()
                {
                    Id = custId, City = "London", Country = "UK"
                });

                db.SubmitChanges();
            }                

            string expectedCity = "New York";
            string expectedCountry = "USA";

            var command = new CustomerMovedCommand();
            command.CustomerId = custId;
            command.NewAddress = new Address()
            {
                City = expectedCity, Country = expectedCountry
            };

            // Act
            command.Execute();

            // Assert
            using (var db = new ContextFactory.CreateContext())
            {
                var c = db.Customers.Single(c => c.Id == custId);

                Assert.AreEqual(expectedCity, c.City);
                Assert.AreEqual(expectedCountry, c.Country);
            }
        } // Dispose rolls back everything.
    }
}

I hope this helps, but next time, please be a little more specific in your question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜