chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
I want to replace a call in my class-under-test to SqlAdatper's Fill method and deal with it in my test code - I intend to put my own values into the DataSet. I understood that DynamicReturnValue would be the thing to use.

I have this mock

Mock SqlDataAdapterMock = MockManager.MockAll<System.Data.SqlClient.SqlDataAdapter>();
SqlDataAdapterMock.ExpectAlways("Fill", new DynamicReturnValue(MockFill));


and deal with it in
public object MockFill(object[] parameters, object context)
{
   DataSet ds;
   // some code to fill in the dataset
   parameters[0] = ds;
   return 0;
}


I guess my use of DynamicReturnValue isn't correct though, at least not with ExpectAlways which doesn't compile. (I have tried other calls e.g. ExpectCall.)

What a I doing wrong?
asked by RAD (1.8k points)

5 Answers

0 votes
Hi,
The method that you want to mock is:
SqlDataAdapter.Fill(DataSet ds);
For this test, you want to fill the DataSet manually, so in the DynamicReturnvValue we can grab the first parameter and set its value.

public object MockFill(object[] parameters, object context)
{
   // grab first argument
   DataSet ds = (DataSet)parameters[0];

   // set fake data on the dataset
   FillFakeDataSet(ds);

   // Fill returns number of fields 
   return 2; 
}


:idea: You can of course Mock the DataSet instead of filling it with fake data.
answered by scott (32k points)
0 votes
I replaced ExpectAlways with ExpectAndReturn and now it compiles. Obvious really.

Thanks.
answered by RAD (1.8k points)
0 votes
Hi,

Now that the you can compile it, does your test run as expected? If you're still having problems, please post the code, so we can help.
answered by gilz (14.5k points)
0 votes
Yes this works fine now. I noticed that I have to use MockAll not Mock which threw me a bit but it does work.

I have not tried mocking a DataSet yet.
answered by RAD (1.8k points)
0 votes
Great!

Let me know if you need additional assistance.
answered by gilz (14.5k points)
...