-
-
Notifications
You must be signed in to change notification settings - Fork 802
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cannot mock a method to return null using LINQ syntax #337
Comments
Since null is the default return value for such a mock, why even try
setting it explicitly?
On Tue, Mar 28, 2017, 9:44 PM mayerc ***@***.***> wrote:
I need the result of a method vary by argument and return null for a
particular argument. I tried the following snippet but Mock.Of fails
(exception details further below.)
var mockUserProvider = Mock.Of<IUserProvider>(p => p.GetUserByEmail("
***@***.***") == new User() && ***@***.***")
== null);
If I remove the second clause of the predicate, then the exception doesn't
happen.
This feels like something that should work. Could someone offer a
work-around?
Not sure if this issue is the same as this:
https://groups.google.com/forum/#!topic/moqdisc/tRYXrkgTjI0
exception details
{"Expression of type 'System.Object' cannot be used for parameter of type
'Kobe.Common.Models.Interfaces.IUserWithDetails' of method
'Moq.Language.Flow.IReturnsResult`1[Kobe.Common.Users.IUserProvider]
Returns(Kobe.Common.Models.Interfaces.IUserWithDetails)'"}
" at System.Linq.Expressions.Expression.ValidateOneArgument(MethodBase
method, ExpressionType nodeKind, Expression arg, ParameterInfo pi)\r\n at
System.Linq.Expressions.Expression.ValidateArgumentTypes(MethodBase method,
ExpressionType nodeKind, ReadOnlyCollection1& arguments)\r\n at
System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo
method, IEnumerable1 arguments)\r\n at
System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo
method, Expression[] arguments)\r\n at
Moq.Linq.MockSetupsBuilder.ConvertToSetup(Expression targetObject,
Expression left, Expression right)\r\n at
Moq.Linq.MockSetupsBuilder.ConvertToSetup(Expression left, Expression
right)\r\n at Moq.Linq.MockSetupsBuilder.VisitBinary(BinaryExpression
node)\r\n at
System.Linq.Expressions.BinaryExpression.Accept(ExpressionVisitor
visitor)\r\n at System.Linq.Expressions.ExpressionVisitor.Visit(Expression
node)\r\n at
System.Linq.Expressions.ExpressionVisitor.VisitBinary(BinaryExpression
node)\r\n at Moq.Linq.MockSetupsBuilder.VisitBinary(BinaryExpression
node)\r\n at
System.Linq.Expressions.BinaryExpression.Accept(ExpressionVisitor
visitor)\r\n at System.Linq.Expressions.ExpressionVisitor.Visit(Expression
node)\r\n at
System.Linq.Expressions.ExpressionVisitor.VisitLambda[T](Expression1
node)\r\n at System.Linq.Expressions.Expression1.Accept(ExpressionVisitor
visitor)\r\n at System.Linq.Expressions.ExpressionVisitor.Visit(Expression
node)\r\n at
System.Linq.Expressions.ExpressionVisitor.VisitUnary(UnaryExpression
node)\r\n at Moq.Linq.MockSetupsBuilder.VisitUnary(UnaryExpression
node)\r\n at
System.Linq.Expressions.UnaryExpression.Accept(ExpressionVisitor
visitor)\r\n at System.Linq.Expressions.ExpressionVisitor.Visit(Expression
node)\r\n at
System.Linq.Expressions.ExpressionVisitor.VisitArguments(IArgumentProvider
nodes)\r\n at
System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression
node)\r\n at
Moq.Linq.MockSetupsBuilder.VisitMethodCall(MethodCallExpression node)\r\n
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor
visitor)\r\n at System.Linq.Expressions.ExpressionVisitor.Visit(Expression
node)\r\n at Moq.Linq.MockQueryable1.Execute[TResult](Expression
expression)\r\n at System.Linq.Queryable.First[TSource](IQueryable1
source, Expression1 predicate)\r\n at Moq.Mock.Of[T](Expression1
predicate)\r\n at
Kobe.Web.Controllers.Api.Tests.UserControllerTests.<Patch_Accepts_Addition_Of_Email_Address_Alias_When_User_Has_No_Aliases>d__17.MoveNext()
in
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#337>, or mute the thread
<https:/notifications/unsubscribe-auth/AAKW60ZqmMhCdWTX0Vhj8BewiW_sLvi9ks5rqaljgaJpZM4MsXcR>
.
--
…--
/kzu from mobile
|
hey kzu, thanks for the quick response.
Because it makes the intent clear in the code, therefore the code is easier to read. It's easier to read for myself but more importantly to members of my team, some of which may not have much experience with Moq. The actual code I'd like to write is more like this:
In the above code, the intent (return "non existing user" when |
@mayerc-MSFT, you can actually do that! public partial class User { }
public interface IUserProvider
{
User GetUserByEmail(string email);
}
…
User nonExistingUser = null;
var provider = Mock.Of<IUserProvider>(p => p.GetUserByEmail("[email protected]") == new User()
&& p.GetUserByEmail("[email protected]") == nonExistingUser);
Assert.IsType<User>(provider.GetUserByEmail("john@contoso.com"));
Assert.Equal(nonExistingUser, provider.GetUserByEmail("[email protected]")); (Don't declare |
This warning is good for now, but you can ignore it with the next release (version >4.7.63) of Moq. |
I need the result of a method vary by argument and return null for a particular argument. I tried the following snippet but Mock.Of fails (exception details further below.)
var mockUserProvider = Mock.Of<IUserProvider>(p => p.GetUserByEmail("[email protected]") == new User() && p.GetUserByEmail("[email protected]") == null);
If I remove the second clause of the predicate, then the exception doesn't happen.
This feels like something that should work. Could someone offer a work-around?
Not sure if this issue is the same as this: https://groups.google.com/forum/#!topic/moqdisc/tRYXrkgTjI0
exception details
{"Expression of type 'System.Object' cannot be used for parameter of type 'Kobe.Common.Models.Interfaces.IUserWithDetails' of method 'Moq.Language.Flow.IReturnsResult`1[Kobe.Common.Users.IUserProvider] Returns(Kobe.Common.Models.Interfaces.IUserWithDetails)'"}
" at System.Linq.Expressions.Expression.ValidateOneArgument(MethodBase method, ExpressionType nodeKind, Expression arg, ParameterInfo pi)\r\n at System.Linq.Expressions.Expression.ValidateArgumentTypes(MethodBase method, ExpressionType nodeKind, ReadOnlyCollection
1& arguments)\r\n at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, IEnumerable
1 arguments)\r\n at System.Linq.Expressions.Expression.Call(Expression instance, MethodInfo method, Expression[] arguments)\r\n at Moq.Linq.MockSetupsBuilder.ConvertToSetup(Expression targetObject, Expression left, Expression right)\r\n at Moq.Linq.MockSetupsBuilder.ConvertToSetup(Expression left, Expression right)\r\n at Moq.Linq.MockSetupsBuilder.VisitBinary(BinaryExpression node)\r\n at System.Linq.Expressions.BinaryExpression.Accept(ExpressionVisitor visitor)\r\n at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)\r\n at System.Linq.Expressions.ExpressionVisitor.VisitBinary(BinaryExpression node)\r\n at Moq.Linq.MockSetupsBuilder.VisitBinary(BinaryExpression node)\r\n at System.Linq.Expressions.BinaryExpression.Accept(ExpressionVisitor visitor)\r\n at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)\r\n at System.Linq.Expressions.ExpressionVisitor.VisitLambda[T](Expression1 node)\r\n at System.Linq.Expressions.Expression
1.Accept(ExpressionVisitor visitor)\r\n at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)\r\n at System.Linq.Expressions.ExpressionVisitor.VisitUnary(UnaryExpression node)\r\n at Moq.Linq.MockSetupsBuilder.VisitUnary(UnaryExpression node)\r\n at System.Linq.Expressions.UnaryExpression.Accept(ExpressionVisitor visitor)\r\n at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)\r\n at System.Linq.Expressions.ExpressionVisitor.VisitArguments(IArgumentProvider nodes)\r\n at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)\r\n at Moq.Linq.MockSetupsBuilder.VisitMethodCall(MethodCallExpression node)\r\n at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)\r\n at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)\r\n at Moq.Linq.MockQueryable1.Execute[TResult](Expression expression)\r\n at System.Linq.Queryable.First[TSource](IQueryable
1 source, Expression1 predicate)\r\n at Moq.Mock.Of[T](Expression
1 predicate)\r\n at Kobe.Web.Controllers.Api.Tests.UserControllerTests.<Patch_Accepts_Addition_Of_Email_Address_Alias_When_User_Has_No_Aliases>d__17.MoveNext() inThe text was updated successfully, but these errors were encountered: