Hi Guys,
I'm attempting to test my repository using an in-memory mock context.
I implement this with an in-memory Dictionary, as most people do. This implements members on my repository interface to Add, Remove, Find, etc, working with the in-memory collection.
This works fine in most scenarios:
[TestMethod]
public void CanAddPost()
{
IRepository<Post> repo = new MockRepository<Post>();
repo.Add(new Post { Title = "foo" });
var postJustAdded = repo.Find(t => t.Title == "foo").SingleOrDefault();
Assert.IsNotNull(postJustAdded); // passes
}
However, i have the following test which i cannot get to pass with the mock repository (works fine for the SQL repository).
Consider i have three repositories:
- Posts (handles user content posts, like a StackOverflow question).
- Locations (locations in the world, like "Los Angeles").
- LocationPosts (junction table to handle many-many between Posts/Locations).
Posts can be added to nowhere, or they can also be added with a particular Location.
Now, here's my test:
[TestMethod]
public void CanAddPostToLocation()
{
var location = locationRepository.FindSingle(1); // Get LA
var post = new Post { Title = "foo", Location = location }; // Create post, with LA as Location.
postRepository.Add(post); // Add Post to repository
var allPostsForLocation = locationPostRepository.FindAll(1); // Get all LA posts.
Assert.IsTrue(allPostsForLocation.Contains(post)); // works for EF, fails for Mock.
}
Basically, when using the "real" EF/SQL Repository, when i add a Post to a particular location, EntityFramework is smart enough to add the "LocationPost" record, because of the association in the EDMX ("LocationPosts" navigational property on "Post" entity)
But how can i make my Mock repository smart enough to "mimic" this EF intelligence?
When i do "Add" on my mock repository, this just adds to the Dictionary. It has no smarts to go "Oh, wait, you have a dependant association, let me add that to the OTHER repository for you".
My Mock Repository is generic, so i dont know how to put the smarts in there.
I have also looked at creating a FakeObjectContext / FakeObjectSet (as advised by Julie Lerman on her blog), but this still does not cover this scenario.
I have a feeling my mocking solution isn't good enough. Can anyone help, or provide an up-to-date article on how to properly mock an Entity Framework 4/SQL Server repository covering my scenario?
The core of the issue is i have one repository per aggregate root (which is fine, but is also my downfall).
So Post and Location are both aggregate roots, but neither "own" the LocationPosts.
Therefore they are 3 seperate repositories, and in an in-memory scenario, they are 3 seperate Dictionaries. I think i'm missing the "glue" between them in my in-memory repo.
EDIT
Part of the problem is that i am using Pure POCO's (no EF code generation). I also do not have any change tracking (no snapshot-based tracking, no proxy classes).
I am under the impression this is where the "smarts" happen.
At the moment, i am exploring a delegate option. I am exposing a event in my Generic Mock Repository (void, accepts generic T, being the Entity) which i invoke after "Add". I then subscribe to this event in my "Post Repository", where i plan to add the related entities to the other repositories.
This should work. Will put as answer if it does so.
However, im not sure it this is the best solution, but then again, this is only to satisfy mocking (code won't be used for real functionality).