C# 为什么会失败?
“消息:Assert.Is True失败。应为:System.Collections.Generic.ListC# 为什么会失败?,c#,visual-studio,C#,Visual Studio,“消息:Assert.Is True失败。应为:System.Collections.Generic.List1[…],但找到了System.Collections.Generic.List1[…]” 我不知道为什么它会失败。有人能帮我解决这个错误吗 /// <summary> /// Test to Get the all the fee bands from Crm /// </summary> ///
1[…],但找到了System.Collections.Generic.List
1[…]”
我不知道为什么它会失败。有人能帮我解决这个错误吗
/// <summary>
/// Test to Get the all the fee bands from Crm
/// </summary>
/// <returns>Response with a collection of ise_feeband entities</returns>
[TestCategory("AnnualBillingService")]
[TestMethod]
public void GetFeeBandingListTest()
{
List<ISE_feeband> fee_bands = new List<ISE_feeband>() { };
//fee_bands.Add(new ISE_feeband());
string entityname = "entity name";
Guid ID = new Guid();
using (ShimsContext.Create())
{
//Arrange
ShimCrmService.AllInstances.FetchString = ((@this, fetchXml) =>
{
return new Microsoft.Xrm.Sdk.EntityCollection()
{
EntityName = entityname,
MoreRecords = true,
MinActiveRowVersion = "version",
PagingCookie = "paging cookie",
TotalRecordCount = 10,
TotalRecordCountLimitExceeded = false,
};
});
//Act
var AnnualBillingService = new AnnualBillingService();
var response = AnnualBillingService.GetFeeBandingList();
//Assert
Assert.IsNotNull(response, "Expected not-null response");
Assert.IsTrue(response.FeeBands == fee_bands, "Expected: " + fee_bands + " but found " + response.FeeBands);
foreach (var FeeBands in fee_bands)
{
Assert.IsTrue(FeeBands.Id == ID, "Expects True");
}
}
}
//
///测试以从Crm获取所有费用级别
///
///使用ise_feeband实体集合进行响应
[测试类别(“年度服务”)]
[测试方法]
public void GetFeeBandingListTest()
{
列表费用=新列表(){};
//费用带。添加(新的费用带());
字符串entityname=“实体名称”;
Guid ID=新Guid();
使用(ShimsContext.Create())
{
//安排
ShimCrmService.AllInstances.FetchString=(@this,fetchXml)=>
{
返回新的Microsoft.Xrm.Sdk.EntityCollection()
{
EntityName=EntityName,
MoreRecords=true,
MinActiveRowVersion=“version”,
PagingCookie=“分页cookie”,
TotalRecordCount=10,
TotalRecordCountLimitExcepended=false,
};
});
//表演
var AnnualBillingService=新的AnnualBillingService();
var response=annualAlingService.GetFeeBandingList();
//断言
IsNotNull(响应,“预期非空响应”);
Assert.IsTrue(response.FeeBands==fee_bands,预期为“+fee_bands+”,但找到“+response.FeeBands”);
foreach(费带中的var费带)
{
Assert.IsTrue(FeeBands.Id==Id,“预期为True”);
}
}
}
这是代码。我无法理解错误,奇怪的是,预期结果和实际结果是一样的,仍然会出现错误 也许您要寻找的是集合的assert方法,用于比较类型集合。
AreEqual
和AreEqual
之间存在差异,我不太确定您的案例是什么,所以请同时查看它们
遵循此链接,您将找到这两种方法及其文档,它应该对您有足够的帮助。
//
///测试以从Crm获取所有费用级别
///
///使用ise_feeband实体集合进行响应
[测试类别(“年度服务”)]
[测试方法]
public void GetFeeBandingListTest()
{
Guid ID=新Guid();
列表费用=新列表();
使用(ShimsContext.Create())
{
//安排
ShimCrmService.AllInstances.FetchString=(@this,fetchXml)=>
{
var output=new Microsoft.Xrm.Sdk.EntityCollection()
{
EntityName=ISE_feeband.EntityLogicalName,
};
output.Entities.Add(新的ISE_feeband()
{
Id=Id,
});
返回输出;
});
//表演
var AnnualBillingService=新的AnnualBillingService();
var response=annualAlingService.GetFeeBandingList();
//断言
IsNotNull(响应,“预期非空响应”);
Assert.IsTrue(response.FeeBands.Count==1,“预期:“+fee_bands+”但找到“+response.FeeBands”);
foreach(费带中的var费带)
{
Assert.IsTrue(FeeBands.ISE_feebandId==ID,“预期为真”);
}
}
}
问题解决了,伙计们,谢谢大家:)请编写代码……猜测一下,您试图断言从测试中调用方法的结果会生成预期的项,但是断言会检查集合是否引用相等,这意味着它们不会相等,因此失败。或者,在您跳过的断言消息中有更多详细信息,可以告诉我们它失败的原因。无论哪种方式,您都需要提供更多信息。现在我们能给出的唯一答案是,它失败是因为你做错了什么。@LasseVågsætherKarlsen比我抢先一步,最有可能的原因是根据
List
的类型参数,你可以使用或构建自己的比较器。我已经更新了有关它的更多信息@MKougiouris
/// <summary>
/// Test to Get the all the fee bands from Crm
/// </summary>
/// <returns>Response with a collection of ise_feeband entities</returns>
[TestCategory("AnnualBillingService")]
[TestMethod]
public void GetFeeBandingListTest()
{
Guid ID = new Guid();
List<ISE_feeband> fee_bands = new List<ISE_feeband>();
using (ShimsContext.Create())
{
//Arrange
ShimCrmService.AllInstances.FetchString = ((@this, fetchXml) =>
{
var output = new Microsoft.Xrm.Sdk.EntityCollection()
{
EntityName = ISE_feeband.EntityLogicalName,
};
output.Entities.Add(new ISE_feeband()
{
Id = ID,
});
return output;
});
//Act
var AnnualBillingService = new AnnualBillingService();
var response = AnnualBillingService.GetFeeBandingList();
//Assert
Assert.IsNotNull(response, "Expected not-null response");
Assert.IsTrue(response.FeeBands.Count == 1, "Expected: " + fee_bands + " but found " + response.FeeBands);
foreach (var FeeBands in fee_bands)
{
Assert.IsTrue(FeeBands.ISE_feebandId == ID , "Expects True");
}
}
}