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
Hi all,

I would like to fake a typed dataset, and its typed row.
I tryed :

MyDataSet.MyDataTable fakeMyDataTable = Isolate.Fake.Instance<MyDataSet>(Members.CallOriginal);
MyDataSet.MyRow fakeRow = Isolate.Fake.Instance<MyDataSet>(Members.CallOriginal);
Isolate.WhenCalled(() => fakeMyDataTable.Rows.Count).WillReturn(1);
Isolate.WhenCalled(() => fakeMyDataTable.Rows[0]).WillReturn(fakeRow);
Isolate.WhenCalled(() => fakeRow.MyValue).WillReturn("my value");

Isolate.Swap.AllInstances<MyDataSet>().With(fakeMyDataTable);


but typemock throws me an error

at System.Data.DataRow..ctor(DataRowBuilder builder)
at MyNamespace.MyDataSet.MyRow..ctor(DataRowBuilder rb) in MyDataSet.Designer.cs: line 1391
at TypeMock.MockManager.b(Type A_0, Object[] A_1)
at TypeMock.MockManager.MockObject(Type type, Constructor mockConstructors, Object[] args)
at TypeMock.MockManager.MockObject<TMockedType>(Constructor mockConstructors)
at TypeMock.ArrangeActAssert.InstanceFakerBase`1.g()
at TypeMock.ArrangeActAssert.InstanceFakerBase`1.e()
at ft.CreateFakeInstance<T>(Members behavior, Constructor constructorFlag, Constructor baseConstructorFlag, Type baseType, Object[] ctorArgs)
at ft.Instance<T>(Members behavior)
at MyUnitTest.MyTest.TestMethod() in MyTest.cs: line 49


Is there a tutorial or best practices to fake a typed dataset ?

Thanks for your help.

(Note : the code tag seems to remove some values in my first code sample).
asked by timor.super (1.9k points)

7 Answers

0 votes
Hi,

Can you please add the exception message to the stack trace posted?

Regards,
Elisha
Typemock Support
answered by Elisha (12k points)
0 votes
in fact, if I remove the Members.CallOriginal, it's working.

Strange.

Anyway, faking typed dataset is very verbose. Do you have best practices for this kind of mocking ?

Best regards
answered by timor.super (1.9k points)
0 votes
Hi,

You must specify the data, but Isolator can help using chain and indexers faking:

MyDataSet fakeDataSet = Isolate.Fake.Instance<MyDataSet>();
Isolate.WhenCalled(() => fakeDataSet.MyDataTable[0].MyValue).WillReturn("my value");
Isolate.WhenCalled(() => fakeDataSet.MyDataTable[1].MyValue).WillReturn("my other value");


This line will fake all the objects in the chain, and in addition will take care of the count so fakeDataSet.MyDataTable.Count will equal 2.

Regards,
Elisha
Typemock Support
answered by Elisha (12k points)
0 votes
if i use this :

Isolate.WhenCalled(() => fakeDataSet.MyDataTable[0].MyValue).WillReturn("my value"); 

I need to cast fakeDataSet.MyDataTable[0] to MyRow

to have

Isolate.WhenCalled(() => ((MyRow)fakeDataSet.MyDataTable[0]).MyValue).WillReturn("my value"); 


But this throws a cast exception.
Maybe MyDataTable[0] is not of type MyRow yet.
answered by timor.super (1.9k points)
0 votes
Hi,

It doesn't work since it cast inside WhenCalled cannot be done. Since it's a the dataset is a recursive fake the row returns DataRow which is not convertible to a typed row.

In this case you must specify the behavior explicitly as in your original test. Can you please describe what are you trying to test? Maybe in a wider context it'll be possible to find better solution.

Regards,
Elisha
Typemock Support
answered by Elisha (12k points)
0 votes
Yes, I can describe :D

After retrieving data from the database (using typed dataset), I create big DTO with this data.
What I would like to test is that the DTO is well constructed.

That's why I would like to fake the database and think about mocking the dataset.
answered by timor.super (1.9k points)
0 votes
Hi,

If you're just testing the state of the DTO, why not use a real dataset and swap it using Isolator? You can create a new dataset and add rows to it, there is no gain in using fake. By using a real dataset you'll gain less fragile test and more readable setup.

Less fragile: The implementation get change and use both the typed row and the non types rows without change in the tests.
More readable: Less behavior sets.

Regards,
Elisha
Typemock Support
answered by Elisha (12k points)
...