Linq: Join or where conditions ignored

As a part of my effort to start writing more tests for my code, I'm using a mocked DataContext which contains Lists of dummy data for use in unit tests.

However, I'm running into the awkward situation where the method I'm testing returns the correct results, but the test code that verifies these results against the dummy DataContext doesn't.

The method I'm testing is: var earns = earnService.GetEarnsForCharacter(character.Id);

This returns a list of denormalized Earn objects with only Id, Title, and Display properties. The actual data entities have many more properties, including the one I'm testing for, which is WorldId. I want the WorldId of the returned Earn objects to be equal to character.WorldId.

I figured I should be able to do something like this:

// Execute the method we're testing
var earns = earnService.GetEarnsForCharacter(character.Id);

// Assert that the character's world id is indeed equal to the world id's of all the selected earns
// First, fetch the earns from the datacontext
var worldIds = from ec in earns join e in mockedDataContext.Object.Get<Earn>() on ec.Id equals e.Id select e.WorldId;
foreach (var worldId in worldIds)
{
    Assert.AreEqual(character.WorldId, worldId);
}

However, for some reason, while the earns variable contains the expected 2 objects, the worldIds enumerable contains 3 objects, no matter what I do. I have tried adding a where clause with where earns.Select(t => t.Id).Contains(e.Id) as well, but this has absolutely no effect on the result either.

Any suggestions are much appreciated!

Answers


If mockedDataContext.Object.Get<Earn>() returns two items with the same Id value then your join will return 3 records instead of the expected two.


Need Your Help

durandal child router with parameter from parent

durandal durandal-2.0

I am trying to initialize a child router to build sub navigation for the customer section of my application.

What is an Activity in Android?

android android-layout android-activity android-studio mobile

I'm a beginner to Android Development, Can anyone please explain what is exactly an 'Activity' means in Android ? Is that similar to a 'Page' of an Android Application ?