2016-11-28 66 views
1

我試圖嘲弄使用起訂量以下的方式嘲諷方法:通用倉庫

public interface IGenericRepository<TEntity> where TEntity : class 
{ 
    ... 

    IEnumerable<TEntity> Get(Expression<Func<TEntity, bool>> filter = null, 
     Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, string includeProperties = ""); 

} 

它的初始化是這樣的:

_invoiceRepository = new SqlGenericRepository<InvoiceEntity>(Context); 

參數的不管,該方法應該總是返回一個列表。

我試圖

_invoiceRepositoryMock.Setup(m => m.Get(It.IsAny<>()).Returns(...) 

_invoiceRepositoryMock.Setup(m => m.Get(It.IsAny<Expression<Func<InvoiceEntity, bool>>>())).Returns(...) 

但都沒有奏效。

回答

1

假設

var _invoiceRepositoryMock = new Mock<InvoiceEntity>(); 

然後設置可以是

_invoiceRepositoryMock 
    .Setup(m => m.Get(
     It.IsAny<Expression<Func<InvoiceEntity, bool>>>(), 
     It.IsAny<Func<IQueryable<InvoiceEntity>, IOrderedQueryable<InvoiceEntity>>>(), 
     It.IsAny<string>())) 
    .Returns(...); 

或多個特定

_invoiceRepositoryMock 
    .Setup(m => m.Get(It.IsAny<Expression<Func<InvoiceEntity, bool>>>(), null, string.Empty)) 
    .Returns(...); 
+0

你的「特定調用」('.Returns'之前)缺少''''。但它的工作。謝謝! – mosquito87

+0

固定。樂意效勞。快樂編碼! – Nkosi

1

,如果你的方法是:

IEnumerable<TEntity> Get(
    Expression<Func<TEntity, bool>> filter = null, 
    Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,  
    string includeProperties = ""); 

你模仿得是這樣的:

_invoiceRepositoryMock.Setup(m => m.Get(
    It.IsAny<Expression<Func<InvoiceEntity, bool>>>(), 
    It.IsAny<Func<IQueryable<InvoiceEntity>, IOrderedQueryable<InvoiceEntity>>>(), 
    It.IsAny<string>())).Returns(...) 
+0

TEntity是在這種背景下是未知的。 – mosquito87

+1

我已經更新了我的答案。顯然,用InvoiceEntity替換TEntity。 –

0

你能請嘗試這樣的,因爲有4個參數的方法,試着給4個參數在模擬也_invoiceRepositoryMock.Setup(m => m.Get(It.IsAny<>(),It.IsAny<>(),It.IsAny<>(),It.IsAny<>()).Returns(...