Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moq dotnetcore - cannot convert from 'method group' to 'Expression<Func<Room, bool>>'

I'm trying to setup Moq in a dotnetcore project. I have a generic repository that is called from one of my controllers. I would like to test this an is using moq to setup the call. But I get an error.

The generic repository call that is initiated so T is Room is:

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

The call from the RoomController looks like:

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

The mocking part looks like:

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

But I keep getting errors:

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>>'

Other posts suggested to add () after It.IsAny<Expression<Func<Room, bool>>>, but this did not help.

Any ideas?

like image 424
Crytrus Avatar asked Sep 02 '25 17:09

Crytrus


1 Answers

params takes an array so change the second expectation to expect any array of expressions 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);
like image 94
Nkosi Avatar answered Sep 04 '25 08:09

Nkosi