我遇到了麻煩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