Skip to content
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

NullReferenceException when returning null from ReturnsAsync #51

Open
GreenDavidA opened this issue Nov 19, 2019 · 2 comments
Open

NullReferenceException when returning null from ReturnsAsync #51

GreenDavidA opened this issue Nov 19, 2019 · 2 comments

Comments

@GreenDavidA
Copy link

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.

@LuizPanariello
Copy link

QueryAsync method isn't working with null, it always expect a IEnumerable object and iterates over it.

At DbDataReaderFactory it needs to do something like this to work:

            var result = resultFunc();
            if (result == null)
                return new DataTableReader(new DataTable());

@balpreetpatil
Copy link

I can confirm, it still has this issue. using Moq.Dapper, Version=1.0.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants