运算符'=='不能应用于类型'Task <(IEnumerable <item>,int)>'和'Task <ienumerable <item >>'

时间:2018-11-12 23:58:32

标签: c# visual-studio msbuild moq

我要嘲笑以下功能。

public interface IRepository
{
    Task<IEnumerable<Item>> GetItems(int total);
}

我的模拟代码是

private readonly IEnumerable<Item> stubList = new List<Item> { new Item { } };

mockRepository = Mock.Of<IRepository>(r => r.GetItems(50) == Task.FromResult(stubList));

它可以在我的桌面(Visual Studio 2017)和Jenkin服务器上的msbuild(MSBuild auto-detection: using msbuild version '15.8.169.51996' from 'C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\bin')上使用。

现在方法已更改为

public interface IRepository
{
    Task<(IEnumerable<Item>, int)> GetItems(int total);
}

并将模拟代码更改为

private readonly IEnumerable<Item> stubList = new List<Item> { new Item { } };

var m = (stubList, 1);
mockRepository = Mock.Of<IRepository>(r => r.GetItems(50) == Task.FromResult(m));

它仍然可以在我的桌面上运行(Visual Studio 2017)。但是msbuild失败并显示以下错误消息?

error CS0019: Operator '==' cannot be applied to operands of type 'Task<(IEnumerable<Item>, int)>' and 'Task<IEnumerable<Item>>'

build.log:

  

CoreResGen:     “ C:\ Program Files \ Microsoft SDKs \ Windows \ v6.0A \ bin \ Resgen.exe” / useSourcePath / r:“ D:\ Jenkins \ workspace ... \ packages \ DocumentFormat.OpenXml.2.8.1 \ lib \ net35 \ DocumentFormat.OpenXml.dll“ /r:C:\Windows\Microsoft.NET\Framework\v2.0.50727\Microsoft.JScript.dll /r:C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib .dll / r:“ C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework \ v3.5 \ System.Core.dll” / r:“ C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework \ v3.5 \ System.Data.DataSetExtensions.dll“ /r:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Data.dll /r:C:\Windows\Microsoft.NET\Framework \ v2.0.50727 \ System.Design.dll /r:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll /r:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System .Drawing.dll / r:“ C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework \ v3.0 \ System.Runtime.Serialization.dll” / r:“ C:\ Program Files(x86)\ Reference程序集\ Microsoft \ Framework \ v3.0 \ System.ServiceModel.dll” / r:C:\ Windows \ Microsof t.NET \ Framework \ v2.0.50727 \ System.Web.Services.dll /r:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Windows.Forms.dll / r:C:\ Windows \ Microsoft.NET \ Framework \ v2.0.50727 \ System.Xml.dll / r:“ C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework \ v3.5 \ System.Xml.Linq.dll” / r: “ C:\ Program Files(x86)\参考程序集\ Microsoft \ Framework \ v3.0 \ WindowsBase.dll” /编译组件\ CheckedComboBox \ PopupComboBox.resx,obj \ Release \ PresentationControls.PopupComboBox.resources组件\ DGV \ DgvDesignerColumnList。 resx,obj \ Release \ Infrastructure.DgvDesignerColumnList.resources Components \ DGV \ frmGridColumnsExt.resx,obj \ Release \ Infrastructure.frmGridColumnsExt.resources Components \ DGV \ dgv.resx,obj \ Release \ Infrastructure.DGV.resources组件\状态.resx,obj \ Release \ Infrastructure.frmChangeGridState.resources Components \ DGV \ frmGridColumns.resx,obj \ Release \ Infrastructure.frmGridColumns.resources Components \ UserControl_Folder.resx,obj \ Release \ Infrastructure.UserControl_ Folder.resources frmDropDownBox.resx,obj \ Release \ Infrastructure.frmDropDownBox.resources frmUsersChangeHistory.resx,obj \ Release \ Infrastructure.frmUsersChangeHistory.resources frmUserPermissions.resx,obj \ Release \ Infrastructure.frmUserPermissions.resource Infrastructure.frmUserRegProdGroups.resources frmErrorBox.resx,obj \ Release \ Infrastructure.frmErrorBox.resources frmGridBox.resx,obj \ Release \ Infrastructure.frmGridBox.resources frmInputBox.resx,obj \ Release \ Infrastructure.frmInputBox.resources frmLongTask。 Release \ Infrastructure.frmLongTask.resources frmNoteBox.resx,obj \ Release \ Infrastructure.frmNoteBox.resources Components \ MonthPicker.resx,obj \ Release \ Infrastructure.MonthPicker.resources frmUserGroups.resx,obj \ Release \ Infrastructure.frmUserGroups.resources resx,obj \ Release \ Infrastructure.frmUsers.resources属性\ Resources.resx,obj \ Release \ Infrastructure.Properties.Resources.resources Reports \ frmEditReports.resx,obj \ Release \ Infr astructure.frmEditReports.resources Reports \ frmJobsMaintenance.resx,obj \ Release \ Infrastructure.frmJobsMaintenance.resources Reports \ frmRunReports.resx,obj \ Release \ Infrastructure.frmRunReports.resources Reports \ frmSelectReports.resx,framRunReports.resources Reports \ frmShowReportLog.resx,obj \ Release \ Infrastructure.frmShowReportLog.resources

2 个答案:

答案 0 :(得分:1)

尝试使用更详细的方法代替LINQ to Mocks

private readonly IEnumerable<Item> stubList = new List<Item> { new Item { } };

//...

var expected = (stubList, 1);

var mock = new Mock<IRepository>();
mock
    .Setup(_ => _.GetItems(50))
    .ReturnsAsync(expected); 

IRepository mockRepository = mock.Object;

//...

该框架可能在尝试使用新语法评估表达式时遇到问题

答案 1 :(得分:0)

当我尝试您的代码时:

my @args = @*ARGS.perl;

我遇到了不同的编译器错误:

  

CS0019运算符'=='不能应用于类型'Task <(IEnumerable,int)>'和'Task <(List stubList,int)>'

的操作数

发生这种情况是因为您比较了class Program { static void Main(string[] args) { var stubList = new List<Item> { new Item() }; var m = (stubList, 1); var mockRepository = Mock.Of<IRepository>(r => r.GetItems(50) == Task.FromResult(m)); } } interface IRepository { Task<(IEnumerable<Item>, int)> GetItems(int total); } class Item { } Task<(List<Item>, int)>Task<(IEnumerable<Item>, int)>不是co-variant(例如与Task<T>相反),因此会出现错误。

但是为什么您的问题包含不同的编译器错误?


如果我将代码更改为此:

List<T>

或者这个:

var m = ((IEnumerable<Item>)stubList, 1);

然后就可以了。