c# - Integration testing multiple Entity framework dbcontexts that share a database -



c# - Integration testing multiple Entity framework dbcontexts that share a database -

in application have multiple little entity framework dbcontexts share same database, example:

public class context1 : dbcontext { public context1() : base("demodb") { } } public class context2 : dbcontext { public context2() : base("demodb") { } }

all database updates done via scripts , not rely on migrations (nor going forward). question - how integration testing against these contexts?

i believe there 3 options here (there may more don't know them)

option 1 - super context - context contains models , configurations required setting database:

public class supercontext : dbcontext { public supercontext() : base("demodb") { } }

in alternative test database setup against super context , subsequent testing done through smaller contexts. reason not keen on alternative duplicating configurations , entity models have built.

option 2 - create custom initialiser integration tests run appropriate db initialisation scripts:

public class integrationtestinitializer : idatabaseinitializer<dbcontext> { public void initializedatabase(dbcontext context) { /* run scripts set database here */ } }

this alternative allows testing against true database construction require updating everytime new db scripts added

option 3 - test individual contexts:

in alternative 1 allow ef create test database based upon context , tests operate within there own "sandbox". reason don't doesn't sense testing against true representation of database.

i'm swaying towards options 2. think? there improve method out there?

i'm using integration testing lot, because still think it's reliable way of testing when data-dependent processes involved. have couple of different contexts, , ddl scripts database upgrades, our situations similar.

what ended option 4: maintaining unit test database content through regular user interface. of course of study integration tests temporarily modify database content, part of "act" phase of test (more on "temporary" later), content not set when test session starts.

here's why.

at stage generated database content @ start of test session, either code or deserializing xml files. (we didn't have ef yet, otherwise have had seed method in database initializer). gradually started sense misgivings approach. hell of job maintain code/xml when info model or business logic changed, esp. when new utilize cases had devised. allowed myself minor corruption of these test data, knowing not impact tests.

also, info had create sense, in had valid , coherent info real application. 1 way ensure generate info application itself, or else inevitably somehow duplicate business logic in seed method. mocking real-world info very hard. that's of import thing found out. testing info constellations don't represent real utilize cases isn't wast of time, it's false security.

so found myself creating test info through application's front end end , painstakingly serializing content xml or writing code generate same. until 1 day occurred me had info readily available in database, why not utilize directly?

now maybe inquire how create tests independent?

integration tests, unit tests, should executable in isolation. should not depend on other tests, nor should affected them. assume background of question create , seed database each integration test. 1 way accomplish independent tests.

but if there 1 database, , no seed scripts? restore backup each test. chose different approach. each integration test runs within transactionscope that's never committed. easy accomplish this. each test fixture inherits base of operations class has these methods (nunit):

[setup] public void inittestenvironment() { setupteardown.pertestsetup(); } [teardown] public void cleantestenvironment() { setupteardown.pertestteardown(); }

and in setupteardown:

public static void pertestsetup() { _tranactionscope = new transactionscope(); } public static void pertestteardown() { if (_tranactionscope != null) { _tranactionscope.dispose(); // rollback changes made in test. _tranactionscope = null; } }

where _tranactionscope static fellow member variable.

c# entity-framework

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -