I have a scenario where the code under test inserts a record into a database, then attempts to retrieve it back from the database using its primary key.
This happens over a series of records in the same method.
I am mocking my ObjectContext and ObjectSets, so the current result is that each record “inserted” into my FakeObjectSet is given a primary key of 0.
In the case where I am only inserting a single record and asserting the record exists, this is fine…but when I am inserting multiple and my workflow requires retrieving specific records by primary key, my queries return multiple results since all inserts have a primary key of 0.
Any ideas?
You can add an event to your
FakeObjectSet<T>that will be raised whenAddObjectis called. Then in your test, add an event handler to set theIdof the object being added.For example, a simplified version of a fake object set:
And in your test, when you want to set an incrementing
Idvalue, add a handler to yourFakeObjectSet<T>and update the object’sId: