与MS Moles一起使用DataContext吗? [英] Moling DataContext with MS Moles?
问题描述
我如何才能在类中使用正在使用的DataContext将消息写到表中.我想断言LINQ正在写入的表具有预期的消息数.这就是我到目前为止所拥有的.
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());
如果我在被测类中将此代码与xUnit一起使用,则会收到此异常
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.
我在这里缺少什么,以及如何在the鼠上实现DataContext.Dispose()?我使用的是没有Pex的痣.
What am I missing here and how to implement DataContext.Dispose() on the mole? I'm using moles standalone without Pex.
推荐答案
创建新的Mole时,其方法和属性的默认行为是在调用它们时抛出MoleNotImplementedException
.
When you create a new Mole the default behavior for its methods and properties is to throw a MoleNotImplementedException
whenever they are called.
要实现mole,您可以执行
我重读了这个问题,由于context.Dispose = () => {};
,这意味着在moled实例上调用Dispose
方法时没有任何反应.Dispose
是在基类中定义的,因此您可能遇到了问题.要使用摩尔基法,您需要执行以下操作:
To implement the mole you can do
I reread the question and you probably are having a problem since context.Dispose = () => {};
which means that nothing happens when the Dispose
method gets called on the moled instance.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 = () => {};
您将需要实现被测试代码调用的每个属性/方法,或者您可以使用方法BehaveAsDefaultValue
全局设置Mole实例的默认行为.这样,痣中的每个方法将不执行任何操作,并返回默认值(如果存在的话)为其返回类型,而不是抛出MoleNotImplementedException
.但是,如果您需要这种行为,最好使用存根而不是葡萄胎.
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.
这篇关于与MS Moles一起使用DataContext吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!