sql server - Can I create an ambient transaction for testing? -


my current system has rather aggressive data layer creates sqldatabase instance me calling static method. pass in stored procedure name (string) , magic happens.

i want try , of crazy system under test , want control in database.

having realised structure

[test] public void should_do_some_thing() {      using (var scope = new transactionscope())             {                 cleanupdatabase();                 setupdatabasedata();                  //run test                  assert.that(someresult,is.equalto("expectedvalue");                  scope.dispose();             } } 

does want (no database changes persist outside test) nicer if set transaction within [setup] method , remove without committing in [teardown] section.

is possible?

note cannot call methods on command object or whatever...

you use testinitialize , testcleanup set up/clean up:

private transactionscope scope;  [testinitialize] public void testinitialize() {     scope = new transactionscope();     cleanupdatabase();     setupdatabasedata(); }  [test] public void should_do_some_thing() {        //run test      assert.that(someresult,is.equalto("expectedvalue"); }  [testcleanup] public void cleanup() {     scope.dispose(); } 

you may need add error handling etc basics of it:

testinitialize:

identifies method run before test allocate , configure resources needed tests in test class. class cannot inherited.

testcleanup:

identifies method contains code must used after test has run , free resources obtained tests in test class. class cannot inherited.

if using nunit can use [setup] , [teardown] instead of [testinitialize] , [testcleanup] respectively.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -