我遇到了麻烦PEX自动覆盖方法调用LINQ的扩展方法,如其中(),包含()在本例中输入:如何获得PEX自动生成代码涉及LINQ
public class MyEntity
{
public int Id { get; set; }
}
public interface IWithQueryable
{
IQueryable<MyEntity> QueryableSet();
}
public class ConsumerOfIhaveIQueryable
{
private readonly IWithQueryable _withIQueryable;
public ConsumerOfIhaveIQueryable(IWithQueryable withIQueryable)
{
// <pex>
Contract.Requires<ArgumentNullException>(
withIQueryable != null, "withIQueryable");
// </pex>
_withIQueryable = withIQueryable;
}
public IEnumerable<MyEntity> GetEntitiesByIds(IEnumerable<int> ids)
{
Contract.Requires<ArgumentNullException>(ids != null, "ids");
// <pex>
Contract.Assert
(this._withIQueryable.QueryableSet() != (IQueryable<MyEntity>)null);
// </pex>
IEnumerable<MyEntity> entities =
_withIQueryable.QueryableSet().Where(
entity => ids.Contains(entity.Id));
if (entities.Count() != ids.Count())
{
return null;
}
return entities;
}
}
[PexClass(typeof(ConsumerOfIhaveIQueryable))]
[PexAllowedExceptionFromTypeUnderTest(typeof(InvalidOperationException))]
[PexAllowedExceptionFromTypeUnderTest(typeof(ArgumentException), AcceptExceptionSubtypes = true)]
[TestClass]
public partial class ConsumerOfIhaveIQueryableTest
{
[PexMethod]
public IEnumerable<MyEntity> GetEntitiesByIds(
[PexAssumeUnderTest]ConsumerOfIhaveIQueryable target,
int[] ids)
{
var result = target.GetEntitiesByIds(ids);
PexAssert.IsTrue(result.Count() == ids.Length);
return result;
}
}
当我在这个PexMethod运行PEX探索我看到以下问题:
- 我不断收到同样的异常和PEX建议保持在Contract.Assert的形式相同的“不变”的修复程序你在//区域看到: 我相信问题是某种程度上与Pex如何与Linq相关,但我不确定
---说明 失败测试:ArgumentNullException,值不能为空。 参数名:源
[TestMethod]
[PexGeneratedBy(typeof(ConsumerOfIhaveIQueryableTest))]
[PexRaisedException(typeof(ArgumentNullException))]
public void GetEntitiesByIdsThrowsArgumentNullException385()
{
using (PexChooseBehavedBehavior.Setup())
{
SIWithQueryable sIWithQueryable;
ConsumerOfIhaveIQueryable consumerOfIhaveIQueryable;
IEnumerable<MyEntity> iEnumerable;
sIWithQueryable = new SIWithQueryable();
consumerOfIhaveIQueryable =
ConsumerOfIhaveIQueryableFactory.Create((IWithQueryable)sIWithQueryable);
int[] ints = new int[0];
iEnumerable = this.GetEntitiesByIds(consumerOfIhaveIQueryable, ints);
}
}
---异常详细
System.ArgumentNullException:值不能为空。 (System.Linq.IQueryable'1 source,System.Linq.Expressions.Expression'1> predicate) c:\ users \ moran \ (System.Collections.Generic.IEnumerable'1 PexIQueryable.ConsumerOfIhaveIQueryable.GetEntitiesByIds(System.Collections.Generic.IEnumerable`1 ids) c(文件\ visual studio 2010 \ Projects \ PexTuts \ :\ users \ moran \ documents \ visual studio 2010 \ Projects \ PexTuts \ PexIQueryable \ PexIQueryable.Tests \ ConsumerOfIhaveIQueryableTest.cs(34):at System.Collections.Generic.IEnumerable'1 PexIQueryable.ConsumerOfIhaveIQueryableTest.GetEntitiesByIds(PexIQueryable.ConsumerOfIhaveIQueryable target, System.Int32 [] ids)
- 我无法让PEX生成相关输入。正如你所看到的,我试图通过在我的代码中添加一个PexAssert和一个分支来“帮助”它,但是这个分支永远不会被覆盖,即使应该是应该相对简单一些来生成一个可以走这条路径的代码。 PEX只会尝试传递null或一个空数组作为Ids列表(我在某处读到PEX更容易处理数组(int [])而不是IEnumerable)
很想得到一些意见这个...
顺便说一句,这是我第一次SO后,希望我没有垃圾邮件太多的代码和信息。
莫兰
我刚刚注意到,当你张贴了这个...我已经过?不过,这是一个有趣的调查! – nicodemus13 2012-04-10 20:11:31