2011-12-11 31 views
14

Tôi đang cố gắng sử dụng Moq để thử giao diện:Setup trong Moq, mơ hồ gọi

public interface IMatchSetupRepository 
{ 
    IEnumerable<MatchSetup> GetAll(); 
} 

và tôi đang làm:

var matchSetupRepository = new Mock<IMatchSetupRepository>(); 
matchSetupRepository 
    .Setup(ms => ms.GetAll()) 
    .Returns(null); 

Nhưng nó thậm chí không biên dịch vì của lỗi:

error CS0121: The call is ambiguous between the following methods or properties: 'Moq.Language.IReturns<Data.Contract.IMatchSetupRepository,System.Collections.Generic.IEnumerable<Data.Model.MatchSetup>>.Returns(System.Collections.Generic.IEnumerable<Data.Model.MatchSetup>)' and 'Moq.Language.IReturns<Data.Contract.IMatchSetupRepository,System.Collections.Generic.IEnumerable<Data.Model.MatchSetup>>.Returns(System.Func<System.Collections.Generic.IEnumerable<Data.Model.MatchSetup>>)'

tôi đang sử dụng:

Moq.dll, v4.0.20926

Trả lời

25

Hãy thử phiên bản chung của Returns:

var matchSetupRepository = new Mock<IMatchSetupRepository>(); 
matchSetupRepository 
    .Setup(ms => ms.GetAll()) 
    .Returns<IEnumerable<MatchSetup>>(null); 

hay:

var matchSetupRepository = new Mock<IMatchSetupRepository>(); 
matchSetupRepository 
    .Setup(ms => ms.GetAll()) 
    .Returns((IEnumerable<MatchSetup>)null); 

Thay vào đó. Bởi vì bạn đang truyền hàm rỗng (và có hai tình trạng quá tải của Returns), trình biên dịch không biết bạn có nghĩa là quá tải nào trừ khi bạn đưa đối số vào đúng loại.

+0

dohh .. thanks man !! hiểu rồi! – user1082693

+0

Không có vấn đề gì - Tôi đã nghĩ đến một cách tốt hơn để làm điều đó, xem bản cập nhật. –

+0

tuyệt vời! Tôi vẫn nhận được sử dụng để moq, tôi sẽ chấp nhận câu trả lời của bạn ngay sau khi SO cho phép nó – user1082693