gt.guybrush
gt.guybrush

Reputation: 1378

Integration Test for Data access Layer with Entity Framework code-first

iam starting writing test for my data access layer and iam looking for best practice.

Now iam testing with a real db wich is auto generated from EF every times it find some differences in my model (defined with code-first fluent mapping) but doing this with many developers is difficult since every ones have different object developed.

1) So there are way to use some in memory database?
a)If don't use a connection string in my test project seen that all works fine but where is this db created?
b) EF create it one time for every test or only when first test start? Can i create it in explicit way?
c) how can this db be deleted?

2) What's i have to test?
a) in my past application i made many error in mapping object property to db column so this seem a thing to test: maybe i can create an object, save it to db, read it from db and test that all property read is equal to what i wrote
b) i think i will use repository pattern, so every object will have own repository with CRUD operations, i have to test this CRUD or it means iam testing EntityFramework itself?
c) after some times i will have many list method in my repository so i will have to insert some fake data in my db and try to query against them to test my method?
d) other things to test?

Thanks

EDIT: i tiny example:

if i map my class:

MyClass{
    public int MyClassIdField
    public string MyClassDescriptionField
}

to table [T01_Table1] with field
int_T01_Table1Id
nvc_T01_Table1Description

i want to define a test for this mapping both for testing correctness and from the point of view of test driven development

Other question: when iam testing inserting operation in db i can use my domain layer object directly or i have to mock them?
without mock i have to use the object constructor that could not work

EDIT 2: in meantime iam using a local db to go on and find other issue: i create a class in this way:

var fixture = new Fixture();
MyClass c = New MyClass();
c.id = fixture.Create<int>();
c.endDate= fixture.Create<DateTime?>();
context.MyClass.Add(c);
context.SaveChanges();

context.Dispose(); // TO force EF query on DB
context= new MyContext();

MyClass actual = context.MyClass.Find(c.Id);

//Assert
actual.DataFine.Value.Should().Be(target.DataFine.Value);

but test fail with this error:
Failure: Expected date and time to be <2014-02-22 16:14:53.901>, but found <2014-02-22 16:14:53.900>.

if don't dispose context the test in successful. how can the data change?

Upvotes: 0

Views: 1392

Answers (1)

cederlof
cederlof

Reputation: 7383

A good rule of thumb when unit testing is not to test code outside of your scope. This would mean not testing the database itself, but mock the database with a mocking library like Moq for example.

Edit: As for integration testing. If you want to test the database itself, you need to use a real database, otherwise you would not test the database engine, but a mocked one, which would miss the target of your test. To use an mdf-file, as you mention, is an approach that would work.

Upvotes: 1

Related Questions