This is my code:
var data = context.GetAll<UserRole>()
.GroupBy(x => new RoleGroup { RoleId = x.RoleId, AccessEnum = x.AccessEnum })
.Where(x => x.Count() > 1);
This is the data mock:
var userRoles = new List<UserRole>
{
new UserRole { Id = 1, RoleId = 1, AccessEnum = AccessEnum.Full },
new UserRole { Id = 2, RoleId = 1, AccessEnum = AccessEnum.Full },
new UserRole { Id = 3, RoleId = 2, AccessEnum = AccessEnum.ReadOnly },
new UserRole { Id = 4, RoleId = 2, AccessEnum = AccessEnum.Full },
new UserRole { Id = 5, RoleId = 2, AccessEnum = AccessEnum.ReadOnly }
};
mockContext.Setup(x => x.GetAll<UserRole>()).Returns(userRoles.AsQuerable());
The GroupBy works fine when using the system. However when I run a unit test it returns each record individually. If I change the GroupBy to an anonymous object, it does work:
.GroupBy(x => new { x.RoleId, x.AccessEnum })
But I need to pass the list into another method. So I need to define the key as a RoleGroup.
Is there a way to get this working by keeping the class in the GroupBy?