Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking Datasets with Moq

Tags:

moq

I'm trying to get started with Moq and having trouble finding any good resources to do what I need.

I have a Data Interface class that has a Get method which returns a Dataset via Stored Procedure. This is the way the code was written and I can't change it at the moment so it has to be done this way.

I want to test this class by Mocking the Dataset and returning data so I don't have to actually make a database call.

Is anyone doing this and if so where is a good place to get started doing it?

like image 492
Adam Avatar asked Feb 23 '09 19:02

Adam


1 Answers

You don't need a database connection to fill in a DataSet. You can mock it like this:

IDataInterface di = new Mock<IDataInterface>();
DataSet mockDataSet = CreateMockDataSet();
di.Expect(x => x.Get()).Returns(mockDataSet);
something.UseDataInterface(di.Object);

Filling in a mock DataSet is quite painful, though. If I'm doing this, I generally put a facade interface in front of the returned DataSet, which is easier to mock. Or I change the code to use a DataTable, which is easier to fill in.

Alternatively, use an embedded database, such as SQLite or SQL Server CE, for your unit tests.

like image 173
Roger Lipscombe Avatar answered Oct 13 '22 14:10

Roger Lipscombe