Unit testing 使用联接时,单元测试错误:对象引用未设置为对象的实例
在Unit testing 使用联接时,单元测试错误:对象引用未设置为对象的实例,unit-testing,join,left-join,nullreferenceexception,Unit Testing,Join,Left Join,Nullreferenceexception,在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()
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:\ChiakiYu\ChiakiYu.DotNetCore\aspnet-core\src\ChiakiYu.Application\AuditLogs\AuditLogAppService.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:\ChiakiYu\ChiakiYu.DotNetCore\aspnet-core\test\ChiakiYu.Tests\Auditing\AuditLogAppService_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();
当我进行单元测试时,出现了一个错误:
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:\ChiakiYu\ChiakiYu.DotNetCore\aspnet-core\src\ChiakiYu.Application\AuditLogs\AuditLogAppService.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:\ChiakiYu\ChiakiYu.DotNetCore\aspnet-core\test\ChiakiYu.Tests\Auditing\AuditLogAppService_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
为空时,joinedUser
为空,joinedUser.NickName
写入错误。可能重复