In my MVC controller ActionResult, I'm trying to do a merge type of query. This works when I run my code, but the Unit test for the ActionResult is failing.
First I get a revResources collection of specific resources, if any. There may be none.
Then I get a list of resources (projResources) joined to the revResources using DefaultIfEmpty.
During testing, I get an object in memory for projResources of type 'System.Linq.IQueryable', but when I try to get a List() I get:
NullReferenceException was unhandled by user code
I'm not sure what's null, or why this works at runtime but not during tests.
During the test, both db.RoleAssignment and db.ProfileSnapshot have valid records that relate correctly. I tested this by trying just select ra or select p.
Here's the code in question:
var revResources = (from ra in db.RoleAssignment
                    where ra.RoleId == (int)SystemRoles.viewReview
                    select ra)
                .Distinct();
// At Runtime this may have zero (0) records and still functions.
var projResources = (from ra in db.RoleAssignment
                        join p in db.ProfileSnapshot on ra.AssigneeId equals p.ProfileSnapshotId
                        join r in revResources on ra.AssigneeId equals r.AssigneeId into rres
                        from r in rres.DefaultIfEmpty()
                        where ra.AssignedToId == review.RequestReview.ProjectSubmissionToReview.ProjectId
                        select new ProfileSnapshotViewModel
                        {
                            ProfileSnapshotId = p.ProfileSnapshotId,
                            Salutation = p.Salutation,
                            FirstName = p.FirstName,
                            MiddleName = p.MiddleName,
                            LastName = p.LastName,
                            Email = p.Email,
                            OriginalSelected = r.RoleAssignmentId != null,
                            Selected = r.RoleAssignmentId != null,
                            RoleAssignmentId = r.RoleAssignmentId
                        }).Distinct();
var assignedResList = projResources.ToList(); // This code fails
The StackTrace in the error detail looks like this:
at lambda_method(Closure , <>f__AnonymousType17`2 )
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.<DistinctIterator>d__81`1.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at PublicationSystem.Controllers.ProjectController.ReviewResourceDetails(Guid id) in c:\Projects\BitLocker\publicationsystem\PublicationSystem\Controllers\ProjectController.cs:line 1200
at PublicationSystem_Test.Controllers.ProjectTests.Project_ReviewResourceDetails_Test() in c:\Projects\BitLocker\publicationsystem\PublicationSystem_Test\Controllers\ProjectTests.cs:line 74
 
     
    