Visual studio 2010 如何使用moles仅调用代表一次/一次?
如何才能使用moles只调用一次/一次委托方法Visual studio 2010 如何使用moles仅调用代表一次/一次?,visual-studio-2010,unit-testing,mocking,moles,pex-and-moles,Visual Studio 2010,Unit Testing,Mocking,Moles,Pex And Moles,如何才能使用moles只调用一次/一次委托方法 MyClass.AllInstances.ResultateGet = delegate { return new ResultatInfoCollection(); }; 我只想调用一次方法“ResultateGet”,因为在没有委托的情况下,init第一次非常复杂 target.UpdateResultate(); //calls delegate "ResultateGet" //Assert some stuff target.Verif
MyClass.AllInstances.ResultateGet = delegate { return new ResultatInfoCollection(); };
我只想调用一次方法“ResultateGet”,因为在没有委托的情况下,init第一次非常复杂
target.UpdateResultate(); //calls delegate "ResultateGet"
//Assert some stuff
target.Verify(); //needs original function "ResultateGet" so unit test is useful
我通常感兴趣的是如何在调用原始函数之前调用moles委托一次或特定次数,而不是委托
target.UpdateResultate(); //calls delegate "ResultateGet"
//Assert some stuff
target.Verify(); //needs original function "ResultateGet" so unit test is useful
更新:
我找到了一个方法,看起来有点麻烦。有更好的解决办法吗
ResultatInfoCollection x = new ResultatInfoCollection();
MolesContext.ExecuteWithoutMoles(() => x = target.Resultate);
另外,请参见我的答案:这提供了匿名方法内部选通逻辑的示例
哦,好问题!我自己也遇到过这种情况。您正在寻找的是所谓的“故障”行为(原始代码的执行)。Moles绕道的匿名方法必须包含一个切换机制,该机制在第一次调用后失效。不幸的是,目前我不相信Moles中包含了故障排除功能
更新后的变通方法正是您所需要的——调用fallthrough也会做同样的事情。我建议添加一个哨兵值doFallthrough,用于屏蔽调用:
bool doFallthrough = false;
ResultatInfoCollection x = new ResultatInfoCollection();
MyClass.AllInstances.ResultateGet = delegate {
if (!doFallthrough)
{
doFallthrough = true;
return new ResultatInfoCollection();
}
MolesContext.ExecuteWithoutMoles(() => x = target.Resultate);
};
调用特定次数只需更改sentinel值类型:
int doFallthrough = 0;
ResultatInfoCollection x = new ResultatInfoCollection();
MyClass.AllInstances.ResultateGet = delegate {
if (++doFallthrough < 5)
return new ResultatInfoCollection();
MolesContext.ExecuteWithoutMoles(() => x = target.Resultate);
};
int-doFallthrough=0;
ResultationInfoCollection x=新ResultationInfoCollection();
MyClass.AllInstances.ResultateGet=委托{
如果(++doFallthrough<5)
返回新的ResultationCollection();
MolesContext.ExecuteWithoutMoles(()=>x=target.Resultate);
};
老问题,但由于我在搜索时找到了它,我将用我的解决方案为下一个人回答
使用MolesContext.ExecuteWithoutMoles
调用原始函数在大多数情况下都可以正常工作,但是,如果您在该调用的下游moling任何其他函数或类,它们也不会被mole
鉴于以下类别:
public class TheClass
{
public int TheFunction(int input){
return input + TheOtherFunction();
}
public int TheOtherFunction(){
return DateTime.Now.Minutes;
}
}
如果使用MolesContext.ExecuteWithoutMoles
方法:
MTheClass.AllInstances.TheOtherFunctionInt = (instance) => {
return 5;
};
MTheClass.AllInstances.TheFunctionInt = (instance, input) =>
{
//do your stuff here, for example:
Debug.WriteLine(input.ToString());
var result = MolesContext.ExecuteWithoutMoles<int>(() => instance.TheFunction(input));
//do more stuff, if desired
return result;
};
MTheClass.AllInstances.TheOtherFunctionInt=(实例)=>{
返回5;
};
MTheClass.AllInstances.TheFunctionInt=(实例,输入)=>
{
//在这里做你的事情,例如:
Debug.WriteLine(input.ToString());
var result=MolesContext.ExecuteWithoutMoles(()=>instance.TheFunction(input));
//如果需要,做更多的事情
返回结果;
};
OtherFunction的mole不会被命中,因为它(间接)是在“无moles”范围内执行的
但是,您可以随时添加和删除moles代理,这样您就可以执行(第24页)中概述的以下操作
MTheClass.AllInstances.TheOtherFunctionInt=(实例)=>{
返回5;
};
MolesDelegates.Func molesDelegate=null;
molesDelegate=(实例,输入)=>
{
//在这里做你的事情,例如:
Debug.WriteLine(input.ToString());
int结果=0;
试一试{
MTheClass.AllInstances.TheFunctionInt=null;
结果=实例。函数(输入);
}
最后{
MTheClass.AllInstances.TheFunctionInt=摩尔当量;
}
//如果需要,做更多的事情
返回结果;
};
MTheClass.AllInstances.TheFunctionInt=摩尔当量;
另一个功能摩尔仍然被击中。使用此方法,您可以仅从特定方法中移除蜕皮,而不会影响其他摩尔。我用过这个,它很管用。我能看到的唯一问题是,如果您有一个递归函数,或者可能是多线程的情况,它将无法工作