使用加入时,单元测试错误:对象引用未设置为对象的实例



GetAuditLogsAsync方法中,我的代码:

    var query = from auditLog in _auditLogRepository.GetAll()
            join user in _userRepository.GetAll() on auditLog.UserId equals user.Id into userJoin
            from joinedUser in userJoin.DefaultIfEmpty()
            select new {AuditLog = auditLog, joinedUser.NickName};
var count = await query.CountAsync();

进行单元测试时,发生了错误:

    System.NullReferenceException : Object reference not set to an instance of an object.
   at lambda_method(Closure , TransparentIdentifier`2 )
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.Count[TSource](IEnumerable`1 source)
   at lambda_method(Closure , QueryContext )
   at Microsoft.EntityFrameworkCore.Storage.Internal.InMemoryDatabase.<>c__DisplayClass8_0`1.<CompileAsyncQuery>b__0(QueryContext qc)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ExecuteAsync[TSource,TResult](MethodInfo operatorMethodInfo, IQueryable`1 source, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.CountAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
   at ChiakiYu.AuditLogs.AuditLogAppService.<GetAuditLogsAsync>d__3.MoveNext() in G:ChiakiYuChiakiYu.DotNetCoreaspnet-coresrcChiakiYu.ApplicationAuditLogsAuditLogAppService.cs:line 64
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Abp.Threading.InternalAsyncHelper.<AwaitTaskWithPostActionAndFinallyAndGetResult>d__5`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at ChiakiYu.Tests.Auditing.AuditLogAppService_Tests.<Should_Get_Audit_Logs>d__2.MoveNext() in G:ChiakiYuChiakiYu.DotNetCoreaspnet-coretestChiakiYu.TestsAuditingAuditLogAppService_Tests.cs:line 75
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

但是,当我修改代码时:

var query = from auditLog in _auditLogRepository.GetAll()
            select new {AuditLog = auditLog};
var count = await query.CountAsync();

还可以。

为什么使用join

发生错误

我该怎么办?谢谢。

我知道,我很愚蠢。当AuditLog.UserId为null时,joinedUser为null,joinedUser.NickName编写不正确。

最新更新