Mocking Dataset with Moq

I am trying to get started with Moq and cannot find any good resources to do what I need.

I have a data interface class that has a Get method that returns a dataset through a stored procedure. This is how the code is written, and I cannot change it at the moment, so it needs to be done this way.

I want to test this class with Mocking the Dataset and return data, so I don't need to actually make a database call.

Does anyone do this, and if so, where is this a good place to start doing this?

+6
moq
source share
1 answer

You do not need a database connection to populate the DataSet. You can mock this as follows:

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

Filling a dummy DataSet is pretty painful. If I do this, I usually put the facade interface in front of the returned DataSet, which is easier to make fun of. Or I change the code to use a DataTable, which is easier to populate.

Alternatively, use a built-in database such as SQLite or SQL Server CE to test the device.

+7
source share

All Articles