0

I am trying to test ASP.NET Core Web API controllers using XUnit. I am using the DBContext directly in the controller. In production, I am using SqlServer and for testing, I am using InMemory provider. When I am testing some code which is using the EF Core FirstOrDefaultAsync method, the application is throwing null reference exception. The value I am looking for doesn't exist in the DB. As per my understanding, it should return NULL, it should not throw an exception.

I tried something like where(x => x.Id = id).FirstOrDefaultAsync(), it is also throwing the same null reference exception.

Null Reference exception

When I tried something like

var exist = await list.AnyAsync(x => x.Id == id);
if(!exist)
{
return NotFound();
}
var user = await list.FirstAsync(x => x.Id == id);
return user;

It works. Both dbcontext and users property are got values, those are not null.

Immediate Window

Please help.

Ivan Stoev
  • 159,890
  • 9
  • 211
  • 258
Anuraj
  • 16,332
  • 6
  • 48
  • 71

1 Answers1

5

The exception is not caused by your code. I was able to reproduce it with the latest at this time EF Core 3.0 Preview 7. It happens with the in-memory provider - FirstOrDefault[Async] and SingleOrDefault[Async] methods. Does not happen with First[Async] or Single[Async].

Anyway, the main problem here is that you are using a preview (beta) software, which is expected to have issues.

Simply switch to the latest stable EF Core 2 version and wait for 3.0 release before trying to use it in production code.

Ivan Stoev
  • 159,890
  • 9
  • 211
  • 258
  • 2
    I am using EF Core v2.1 and got the very same issue as the OP – M. Ruiz Oct 11 '19 at 21:21
  • @M.Ruiz I doubt it is the same - this was really 3.0 preview related and wan't reproducing with the latest stable 2.x. Might be something similar though. Consider asking own question with details about your issue. – Ivan Stoev Oct 14 '19 at 15:31
  • 2
    Excuse me but I'm not asking any question, I'm stating that I have the same issue using v2.1 which is LTS, or at least that's what I thought. Cheers mate. – M. Ruiz Oct 14 '19 at 16:44
  • 1
    I'm having the same issue as well. Using v2.2. – Laurie Dickinson Oct 24 '19 at 20:31
  • Same issue here, I did ask a question about it here on stackoverflow a few days ago, didn't get any answers though, however I did add some code to reproduce it. >https://stackoverflow.com/questions/59500303/why-is-test-failing-when-using-firstordefaultasync-in-method – Noob Dec 30 '19 at 15:30
  • @Noob You are mixing EF Core specific extension method (supposed to work only with EF Core provided `IQueryable<>` implementations) with mocked implementation. I have no idea what that mocking library is doing and what EF Core expects. If the standard sync methods (`First`, `FirstOrDefault` etc.) work and Async versions fail, then I guess the problem is in mocking library implementation. – Ivan Stoev Dec 30 '19 at 15:47
  • @IvanStoev ow, I'll try asking the maker of the library then, maybe he has some examples how to do it properly using firstordefaultasync. Anyway thanks. – Noob Dec 30 '19 at 15:57