2017-04-16 57 views
2

我想在dotnetcore項目中設置Moq。我有一個從我的控制器中調用的通用存儲庫。我想測試這是一個使用moq來設置呼叫。但是我得到一個錯誤。Moq dotnetcore - 無法從'方法組'轉換爲'表達式'

時啓動如此TRoom通用庫調用:

public T GetSingle(Expression<Func<T, bool>> predicate, params 
Expression<Func<T, object>>[] includeProperties) 

RoomController呼叫的樣子:

var room = _roomRepository.GetSingle(r => r.Id == id, ro => ro.Image); 

嘲諷的部分看起來像:

roomRepositoryMock.Setup(c => c.GetSingle(It.IsAny<Expression<Func<Room, bool>>>(), It.IsAny<Expression<Func<Room, object>>>())) 
          .Returns<Room>(null); 

但我不斷收到錯誤:

RoomControllerTest.cs(62,55): error CS1503: Argument 1: cannot convert from 'method group' to 'Expression<Func<Room, bool>>' 
RoomControllerTest.cs(62,70): error CS1503: Argument 2: cannot convert from 'System.Linq.Expressions.Expression<System.Func<viten_i_senter_backend.Models.Room, object>>' to 'System.Linq.Expressions.Expression<System.Func<viten_i_senter_backend.Models.Room, object>>' 

其他職位建議It.IsAny<Expression<Func<Room, bool>>>後添加(),但這並沒有幫助。

任何想法?

回答

2

params帶有一個數組,從而改變第二期望期望表達式的任意陣列It.IsAny<Expression<Func<Room, object>>[]>()

roomRepositoryMock 
    .Setup(c => c.GetSingle(It.IsAny<Expression<Func<Room, bool>>>(), It.IsAny<Expression<Func<Room, object>>[]>())) 
    .Returns<Room>(null); 
相關問題