Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning the value that was passed into a mocked method

I have a method on an interface:

Someclass DoSomething(Someclass whatever);

I can't do .Returns(x => x) because of the error:

cannot convert lambda expression to type Someclass because it is not a delegate type.

Any ideas?

like image 658
Bleh Avatar asked Apr 19 '17 21:04

Bleh


1 Answers

Take a look at these method overloads:

Returns(TResult value)
Returns<T>(Func<T, TResult> valueFunction)

It seems quite obvious for you that C# compiler should pick second overload. But actually C# compiler is unable to infer type of generic parameter T from argument x => x which you are passing. That's why compiler picks non-generic version of method and tries to convert lambda to SomeClass.

To fix this issue you should help compiler to infer type of generic parameter T. You can do it by explicitly specifying type of delegate argument:

.Returns((SomeClass x) => x);

Or you can specify type of T manually

.Returns<SomeClass>(x => x)
like image 136
Sergey Berezovskiy Avatar answered Oct 04 '22 22:10

Sergey Berezovskiy