I’m writing NUnit tests for a service.
I’ve decided to do this by sending direct SQL statements to the database instead of using the Entity Framework model so that I could be certain of the results, i.e. I am testing what is in the database and not what the Entity Framework tells me is in the database (e.g. it could be reporting a cached result, etc.)
The only drawback is that it is getting tedious to write this code using SqlCommand, SqlDataReader, etc. and being able to use the EF model would be much easier.
How are others doing this? Is it good practice to use Entity Framework when writing tests or should direct calls to the database be used to ensure accurate results?
I would say it is a bad practice when testing the service cause it creates a dependency on the database.
What I did, was roll up my Model classes into a Repository pattern. The repository pattern was coded to an interface so that I could change the implementation of the database querying without effecting other parts of the code. It could be easily mocked and used for testing. This is the interface I constructed.