I'm trying to unit test my Data Access Layer and I'm in the process of trying to mock my data connectivity to unit test my DAL and I'm coming unstuck trying to mock out the creation of the commands. I thought about using a queue of IDbParameters for the creation of the parameters, but the unit tests then require that the parameters are configured in the right order. I'm using MOQ and having looked around for some documentation to walk me through this, I'm finding lots of recommendation not to do this, but to write a wrapper for the connection, but it's my contention that my DAL is supposed to be the wrapper for my database and I don't feel I should be writing wrappers... if I do, how do I unit test the connectivity to the database for my wrapper? By writing another wrapper? It seems like it's turtles all the way down.
So does anyone have any recommendations or tutorials regarding this particular area of unit testing/mocking?
preguntado el 09 de enero de 11 a las 01:01
I've had good success swapping an in-memory database. This gives you the opportunity to set it up and tear it down easily. You can also mock the connection call so you can assert the arguments passed in, and then return the in-memory database.
Moq uses inheritance to create a mocked class - the downside of this approach is that the class you're trying to fake should either be passed as an interface at least won't be a sealed class. One solution to is to create a wrapper class around the "un-mockable" class and mock that class instead, unfortunately this solution needs quite a lot of maintenance.
If you're willing to pay for a mocking framework that would enable you to mock sealed classes consider purchasing either Aislador Typemock or JustMock - both can mock virtually any .NET class and method (including sealed classes and static methods).
I'd recommend against using mocks for this type of testing. I went down this path with Java and it always ended with me feeling that I have proved little except that I can write mocking statements. I would recommend either using in-memory database or having a per-developer database and accept some things are integration tests.
I moved to .NET reciently and here a wrapper around
Oracle.DataAccess is used and to be honest, I love this approach.
The wrapper simply provides an IDatabase interface with a bunch of methods like
DataTable ExecuteFunction(...) which offer an easy way to call a oracle function or procedure. The only class from System.Data seen by the clients of this API is DataTable. The wrapper handles all the messing about with IDbParameter. The wrapper only has integration tests.
Unit testing the actual DAL is very easy, plus writing the DAL gets that little bit nicer. Less horrid boilerplate to deal with.
contention that my DAL is supposed to be the wrapper for my database
The DAL provides access to business functions held in the database. The DB wrapper strips away boiler plate code (and allows you to mock it).
if I do, how do I unit test the connectivity to the database for my wrapper?
Don't. Accept that some things are integration tests.