2013-01-24 41 views
6

Tôi đang cố gắng để viết một bài kiểm tra đơn vị cho một phương pháp mà trông như thế này:Thẩm định một danh sách liệt kê trong Moq

public int Save(IEnumerable<int> addedIds, IEnumerable<int> removedIds) 
{ 
    var existingIds = repository.Get(); 
    IEnumerable<int> ids = existingIds.Except(removedIds).Union(addedIds)); 
    return repository.Create(ids); 
} 

Các thử nghiệm trong Moq trông như thế này:

repository.Setup(r => r.Get()).Returns(CreateList()); 
service.Save(addedIds, removedIds); 
repository.Verify(r => r.Create(It.Is<IEnumerable<int>>(l => VerifyList(l)))); 

này thất bại , với lỗi này , và VerifyList() không bao giờ được gọi là:

dự kiến ​​gọi trên giả ít nhất một lần, nhưng không bao giờ được thực hiện :

r => r.Create(It.Is<IEnumerable'1>(list => VerifyList(list)))

lời gọi Thực hiện:

IRepo.Create(System.Linq.Enumerable+<UnionIterator>d__88'1[System.Int32])

Như kiểu gọi không phải là IEnumerable<int> nhưng là trên thực tế System.Linq.Enumerable+<UnionIterator>d__88'1[System.Int32]), thử nghiệm thất bại. (Bước qua kiểm tra, tất cả mọi thứ đang diễn ra một cách chính xác và kết quả là như dự kiến)

Nếu tôi gọi ids.ToList() trong phương thức dưới kiểm tra, đây là những kết quả:

dự kiến ​​gọi trên giả ít nhất một lần , nhưng không bao giờ được thực hiện:

r => r.Create(It.Is<List'1>(l => VerifyList(l)))

lời gọi thực hiện: IRepo.Create(System.Collections.Generic.List'1[System.Int32])

Có cách nào vòng này không? Hay tôi đang làm gì sai?

Chỉnh sửa: nó chỉ ra tôi đã có một sai lầm trong phương pháp VerifyList của tôi vì vậy nó đã trở về sai, nhưng Moq đã không cho tôi thông tin đó. Sự khác biệt loại là một cá trích đỏ ..

Trả lời

8

Điều này dường như làm việc. Thực hiện một số giả định mặc dù. Đoán phương thức VerifyList có thể tốt hơn. =)

[Test] 
    public void Test() 
    { 
     // SETUP 
     Mock<IRepository> repository = new Mock<IRepository>(); 
     Service service = new Service(repository.Object); 
     repository.Setup(r => r.Get()).Returns(CreateList()); 

     IEnumerable<int> addedIds = new[]{1,2}; 
     IEnumerable<int> removedIds = new[]{3,4}; 
     service.Save(addedIds, removedIds); 

     repository.Verify(r => r.Create(It.Is<IEnumerable<int>>(l => VerifyList(l)))); 
    } 

    private static bool VerifyList(IEnumerable<int> enumerable) 
    { 
     return enumerable.Contains(1) && enumerable.Contains(2) && enumerable.Contains(5); 
    } 

    private IEnumerable<int> CreateList() 
    { 
     return new[] { 3, 4, 5 }; 
    } 

    public interface IRepository 
    { 
     IEnumerable<int> Get(); 
     int Create(IEnumerable<int> id); 
    } 
    public class Service 
    { 
     public Service(IRepository repository) 
     { 
      this.repository = repository; 
     } 

     private IRepository repository; 

     public int Save(IEnumerable<int> addedIds, IEnumerable<int> removedIds) 
    { 
     var existingIds = repository.Get(); 
      IEnumerable<int> ids = existingIds.Except(removedIds).Union(addedIds); 

     return repository.Create(ids); 
    } 
+0

Cảm ơn, kiểm tra đó không phù hợp với phiên bản Moq của tôi. Tôi vẫn không hiểu tại sao tôi lại thất bại. – stuartd

+0

Thật lạ lùng. Bạn có thể cập nhật Moq? Bạn đang sử dụng phiên bản Moq nào? – Markus

+0

Tôi đã gặp lỗi trong phương thức VerifyList của mình, điều mà hóa ra nó đã được gọi sau khi tất cả. Cảm ơn sự giúp đỡ của bạn .. – stuartd

2

Something nhanh chóng và dơ bẩn -

public interface IBlah 
{ 
    void Sum(IEnumerable<int> baz); 
} 

class Blah : IBlah 
{ 
    public void Sum(IEnumerable<int> baz) 
    { 
     return; 
    } 
} 

public class Baz 
{ 
    private readonly IBlah blah; 

    public Baz(IBlah blah) 
    { 
     this.blah = blah; 
    } 

    public void Sum(IEnumerable<int> baz) 
    { 
     blah.Sum(baz); 
    } 
} 

và thử nghiệm nó thích -

[Test] 
public void foo() 
{ 
    var mock = new Mock<IBlah>(); 
    var enumerable = Enumerable.Range(1, 10); 

    var baz = new Baz(mock.Object); 
    baz.Sum(enumerable.Where(x => x%2 == 0)); 

    mock.Verify(p => p.Sum(It.Is<IEnumerable<int>>(z => z.All(x => x%2==0)))); 
} 
Các vấn đề liên quan