0

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?

3
  • Usually, in tests that involve database communication, the database is mocked so you don't have to work with a real database Commented Apr 28, 2014 at 11:17
  • ok, but how i can mock a DB and test that my repository work = save data and read data? Commented Apr 28, 2014 at 11:29
  • and how i can test field mapping from object to db without a db? Commented Apr 28, 2014 at 11:30

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

12 Comments

but my scope is database itself: how can i test if table are created correctly with a mock db?
Well in that case you can not use a mock db. It the keyword you are looking for is "integration test" and not "unit test". See for example: tech.trailmax.info/2014/03/…
correct, i edit the title. Interesting article but don't find the way to have a db for each developer. I have created a mdf file in my test project but don't know connection string to use
and if possible i will prefer to use an in-memory db. iam exploring "effort" to see if can do the work
@gt.guybrush If you use sql server, there was no in-memory option until SQL Server 2014 was released. With SQL Server 2014 you can have in-memory database. However, I have not tried that, only read articles about this feature.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.