Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moling DataContext with MS Moles?

How can I mole the DataContext that I'm using in a class to write messages to a table. I'd like to assert that the table LINQ is writing to has the expected count of messages. Here's what i have so far.

var context = new MJustTestingDataContext();
MyMessagewriter writer = new MyMessageWriter(context);

var messageList = new List<MIncmoingMessage>();
MTable<MIncomingMessage> messageTable = new MTable<MIncomingMessage>();
messageTable.Bind(messagesLinqList.AsQueryable());

If I use this code with xUnit in my class under test I'll get this exception

Microsoft.Moles.Framework.Moles.MoleNotImplementedException: DataContext.Dispose() was not moled.

What am I missing here and how to implement DataContext.Dispose() on the mole? I'm using moles standalone without Pex.

like image 893
devployment Avatar asked Sep 10 '10 14:09

devployment


1 Answers

When you create a new Mole the default behavior for its methods and properties is to throw a MoleNotImplementedException whenever they are called.

To implement the mole you can do context.Dispose = () => {}; which means that nothing happens when the Dispose method gets called on the moled instance. I reread the question and you probably are having a problem since Dispose is defined in a base class. To mole base method you need to do the following:

var context = new MJustTestingDataContext();
var baseContext = new MDataContext(context);

baseContext.Dispose = () => {};

You'll need to implement every property/method that gets called by the code under test or you can set the default behavior for the mole instance globally using the method BehaveAsDefaultValue. This way every method in the mole will do nothing and return the default value for it's return type if one exists instead of throwing a MoleNotImplementedException. However if you require this behavior it's better to use a stub than a mole.

like image 55
João Angelo Avatar answered Sep 30 '22 14:09

João Angelo