2014-10-29 9 views
1

PredicateBuilder를 구현하고 간단한 테스트를 만들려고합니다. 내가하지 '필터'는 기록과 컬렉션이 같은 요소를 포함 않습니다 테스트를 실행하고 이런 일이 왜 내가 볼 수 없을 때 나는 ListPredicateBuilder가 모든 결과를 반환하고 원인을 찾을 수 없습니다.

문제에 대해 테스트하고 있습니다 때문에 현재 PredicateBuilderDelegate을 사용하고 입니다 .

FakeEntity 등급 :

public class FakeEntity { 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public bool IsDeleted { get; set; } 
    public static Func<FakeEntity, bool> NotDeleted() { 
     var predicate = PredicateBuilderDelegate.True<FakeEntity>(); 
     predicate.And(e => !e.IsDeleted); 
     return predicate; 
    } 
} 

테스트 :

[TestClass] 
public class PredicateBuilderTest { 
    [TestMethod] 
    public void Test() { 
     var collection = new List<FakeEntity>() { new FakeEntity { IsDeleted = true }, new FakeEntity { IsDeleted = false } }; 
     var result = collection.Where(FakeEntity.NotDeleted()).ToList(); 
     CollectionAssert.AreNotEquivalent(collection, result); // FAIL! 
     Assert.IsTrue(result.Count() == 1); // FAIL !! 

    } 
} 

PredicateBuilderDelegate :

public static class PredicateBuilderDelegate { 
    public static Func<T, bool> True<T>() { return f => true; } 
    public static Func<T, bool> False<T>() { return f => false; } 

    public static Func<T, bool> Or<T>(this Func<T, bool> expr1, 
             Func<T, bool> expr2) { 
     return t => expr1(t) || expr2(t); 
    } 

    public static Func<T, bool> And<T>(this Func<T, bool> expr1, 
             Func<T, bool> expr2) { 
     return t => expr1(t) && expr2(t); 
    } 
} 

답변

3

And 메서드 은 새 조건부을 반환합니다. 은 변경 될 술어을 변경하지 않습니다. (대리자는 결국 변경할 수 없습니다.) 메서드의 반환 값을 무시합니다.

+0

beautifull! 고맙습니다. –