Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在moq中传递谓词函数参数失败_C#_Visual Studio 2013_Integration Testing_Moq_Mef - Fatal编程技术网

C# 在moq中传递谓词函数参数失败

C# 在moq中传递谓词函数参数失败,c#,visual-studio-2013,integration-testing,moq,mef,C#,Visual Studio 2013,Integration Testing,Moq,Mef,我试图使用moq来模拟我的license类上的函数 许可证类别具有以下接口: Licence TryGetLicence(Predicate<Licence> filter); var lic = new Licence { LicId = Guid.Parse("53024D4E-3A01-4489-A341-753D04748EB9"), LicName = "test", Count = 1, ExpiryDate = DateTime.Now.Add

我试图使用moq来模拟我的license类上的函数

许可证类别具有以下接口:

Licence TryGetLicence(Predicate<Licence> filter);
var lic = new Licence
{
   LicId = Guid.Parse("53024D4E-3A01-4489-A341-753D04748EB9"),
   LicName = "test",
   Count = 1,
   ExpiryDate = DateTime.Now.AddDays(2)
};

var mockAgent = new Mock<ILicenceAgent>();
mockAgent.Setup(x => x.TryGetLicence (y => y.LicId == lic.LicId)) Returns(lic);
license TryGetLicence(谓词过滤器);
在我的集成测试中,我使用mef延迟加载对象。我的类找到mef加载的对象,需要检查是否有可用的许可证。对于我的测试,我创建了两个对象,其中一个可以被许可。我想使用moq只返回这个对象的许可证,而另一个对象返回null,就像真正的类一样。我遇到的问题是moq不喜欢我传入谓词。我不确定moq是不是不能以这种方式处理谓词,还是我实现了这个错误

以下是我在测试中为上述接口设置moq的代码行:

Licence TryGetLicence(Predicate<Licence> filter);
var lic = new Licence
{
   LicId = Guid.Parse("53024D4E-3A01-4489-A341-753D04748EB9"),
   LicName = "test",
   Count = 1,
   ExpiryDate = DateTime.Now.AddDays(2)
};

var mockAgent = new Mock<ILicenceAgent>();
mockAgent.Setup(x => x.TryGetLicence (y => y.LicId == lic.LicId)) Returns(lic);
var lic=新许可证
{
LicId=Guid.Parse(“53024D4E-3A01-4489-A341-753D04748EB9”),
LicName=“测试”,
计数=1,
expireydate=DateTime.Now.AddDays(2)
};
var mockAgent=new Mock();
mockAgent.Setup(x=>x.TryGetLicence(y=>y.LicId==lic.LicId))返回(lic);
这将生成,但当命中最后一行时,将抛出不受支持的表达式异常

对于我使用的其他测试:

mockAgent.Setup(x => x. TryGetLicence (It.IsAny<Predicate<Licence>>())).Returns(lic);
mockAgent.Setup(x=>x.TryGetLicence(It.IsAny()).Returns(lic);
我不能将其用于我的新测试,因为它将返回我加载的两个对象的有效许可证


moq可以按照我尝试使用的方式使用吗?

如果您有两个许可证-lic1和lic2,您可以这样设置mock:

mockAgent.Setup(x => x.TryGetLicence(It.Is<Predicate<Licence>>(/* add your specific condition for licence1 */ ))).Returns(lic1);
mockAgent.Setup(x => x.TryGetLicence(It.Is<Predicate<Licence>>(/* add your specific condition for licence2 */ ))).Returns(lic2);
mockAgent.Setup(x=>x.TryGetLicence(它是(/*添加许可证的特定条件1*/)).Returns(lic1);
mockAgent.Setup(x=>x.TryGetLicence(它是(/*添加许可证的特定条件2*/)).Returns(lic2);

这应该可以解决问题:

Predicate<Licence> predicate = y => y.LicId == lic.LicId;
mockAgent.Setup(x => x.TryGetLicence (predicate)).Returns(lic);
谓词=y=>y.LicId==lic.LicId;
mockAgent.Setup(x=>x.TryGetLicence(谓词)).Returns(lic);

Setup
调用中创建谓词时,lambda表达式将作为表达式的一部分进行计算,该表达式将作为参数传递。我们通过确保上面一行中只有一名代表来防止这种情况。

谢谢,这就是我需要它做的。节省了我很多时间。