You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to mock out a dependency that uses Dapper, and I want it to return null. Normally, when I am using Moq, I will cast the null as the return type, and it works. When I do that here, I am getting a System.NullReferenceException.
The code is as follows:
[Fact]
public async Task Test1()
{
Mock<IDbConnection> connection = new Mock<IDbConnection>();
connection
.SetupDapperAsync(m => m.QueryAsync<SampleTable>(It.IsAny<string>(), null, null, null, null))
.ReturnsAsync((IEnumerable<SampleTable>)null);
var actual = connection.Object.QueryAsync<SampleTable>("").GetAwaiter().GetResult().ToList();
}
The exception returned is: Message: System.NullReferenceException : Object reference not set to an instance of an object. Stack Trace: DbDataReaderFactory.DbDataReader[TResult](Func1 result)
<>c__DisplayClass2_01.<SetupQueryAsync>b__1() <>c__DisplayClass2_02.b__1()
--- End of stack trace from previous location where exception was thrown ---
Extensions.InvokePreserveStack(Delegate del, Object[] args)
ReturnLazyValueResponse.RespondTo(Invocation invocation)
MethodCall.Execute(Invocation invocation)
FindAndExecuteMatchingSetup.Handle(Invocation invocation, Mock mock)
IInterceptor.Intercept(Invocation invocation)
Interceptor.Intercept(IInvocation invocation)
AbstractInvocation.Proceed()
DbCommandProxy.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
SqlMapper.QueryAsync[T](IDbConnection cnn, Type effectiveType, CommandDefinition command) line 419
TableDomainTests.Test1() line 148
--- End of stack trace from previous location where exception was thrown ---`
I do have a workaround. In place of returning a null, I return an empty List and it does work.
[Fact]
public async Task Test1()
{
Mock<IDbConnection> connection = new Mock<IDbConnection>();
connection
.SetupDapperAsync(m => m.QueryAsync<SampleTable>(It.IsAny<string>(), null, null, null, null))
.ReturnsAsync(new List<SampleTable>());
var actual = connection.Object.QueryAsync<SampleTable>("").GetAwaiter().GetResult().ToList();
}
I just wanted to bring it to your attention.
The text was updated successfully, but these errors were encountered:
I am trying to mock out a dependency that uses Dapper, and I want it to return null. Normally, when I am using Moq, I will cast the null as the return type, and it works. When I do that here, I am getting a
System.NullReferenceException
.The code is as follows:
The exception returned is:
Message: System.NullReferenceException : Object reference not set to an instance of an object. Stack Trace: DbDataReaderFactory.DbDataReader[TResult](Func
1 result)<>c__DisplayClass2_0
1.<SetupQueryAsync>b__1() <>c__DisplayClass2_0
2.b__1()--- End of stack trace from previous location where exception was thrown ---
Extensions.InvokePreserveStack(Delegate del, Object[] args)
ReturnLazyValueResponse.RespondTo(Invocation invocation)
MethodCall.Execute(Invocation invocation)
FindAndExecuteMatchingSetup.Handle(Invocation invocation, Mock mock)
IInterceptor.Intercept(Invocation invocation)
Interceptor.Intercept(IInvocation invocation)
AbstractInvocation.Proceed()
DbCommandProxy.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
SqlMapper.QueryAsync[T](IDbConnection cnn, Type effectiveType, CommandDefinition command) line 419
TableDomainTests.Test1() line 148
--- End of stack trace from previous location where exception was thrown ---`
I do have a workaround. In place of returning a null, I return an empty
List
and it does work.I just wanted to bring it to your attention.
The text was updated successfully, but these errors were encountered: