Using C# and Entity Framework I've been playing around with some architecture ideas and running into a wall. My question here is How do I unit test the following structure? but perhaps I should be posting on the Software Engineering StackExchange and asking Is this a good architectural approach?
The Idea is that Entity Framework handles ID generation, and can create instances of classes with those IDs set, even if the setter is private
public class MyClass
{
public int Id { get; private set; }
public int MyValue { get; set; }
}
This way other code can never overwrite an Id value that should be database generated, new entries for the database must be created as new instantiations (rather than changing an Id to 0 or other questionable behaviours), etc.
But I'm having trouble using these classes in Unit Tests now. Previously I would mock the Database context so that requesting a table from the context just returned an in-memory collection of the expected model class, with properties like Id
configured so other operations like .Where(i => i.Id == reqestedId)
would work as expected:
mockContext.SetUp(c => c.MyClasses).Returns(myMockCollection);
How do I use those models to help test other parts of the application? Creating interfaces for each model data class seems excessive. Is specifying an internal
setter and making the class visible to the testing project the best approach?