Unit Test database--using things to roll back tests

Source: Internet
Author: User

If you want to ensure the correctness of the business layer test, then we have to test the database. But at the moment I have not thought in memory to carry on the database test, only then carries on the integration test. So here's a look at the data layer test.

Because within the database we have no control we can only pass the black box test, give the value and then return the desired effect we want to determine whether it is successful. But in the test we have to ensure the uniqueness, metaphor in the test add when we add a piece of data then the database may produce a dirty data, if run once a day so the consequences are also terrible, but some people say that the region to add and delete together test, I think it is unreasonable, Because then you have a test both add and delete undesirable. Besides, if it's a change, what do you do? So you need to take a rollback mechanism when testing.

Rolling back data modifications using TransactionScope

Let's look at an example

First we define a TransactionScope

Private TransactionScope _scope;

And then initialize it in Setup.

[SetUp]          Public void SetUp ()        {            thisnew  TransactionScope (transactionscopeoption.required);        }

And then we can test to see the code below

        [Test]        publicvoid  create_createsuccessful_returnstrue ()        {             var userInfo = createuserinfo ();             var New Userdal ();             bool result = userdal.create (userInfo);            Assert.istrue (result);        }

Finally, we release this thing in the teardown.

[TearDown]          Public void TearDown ()        {            this. _scope. Dispose ();        }

Ok so we have finished testing the data layer. Let's look at the results of the test

But there are some references to SOA that we just need to make sure we have the right configuration.

Source: http://www.cnblogs.com/LipeiNet/p/5062464.html

Unit Test database--using things to roll back tests

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.