I'm new to mocking and am playing with JustMock to mock Entity Framework 4. Therefore, my understanding of what I want to happen may be a reflection of my lack of understanding of mocking principles/implementation.
I have browsed around and found some resources, but they're not all that clear. Have seen:
What I want to do is establish a mock of my EF entities, and access them without database.
So far, I have:
var entitiesMock = Mock.Create<Entities>();
This still wants the ConnectionString to appear in the app.config file of the Test DLL, which I would hope wouldn't be required? Without it, the default constructor throws an exception. Question 1:
So am I right in thinking any unit tests I mock in this way are dependant on the DB - sort of making it a bit pointless?
My next task is to create an object and add it to the mocked entities:
UserAccount userAccount =
List<UserAccount> userAccounts =
Mock.Arrange(() => entitiesMock.AddToUserAccounts(Arg.IsAny<UserAccount>())).DoInstead(()=>userAccounts.Add(userAccount));
I understand this, and my object does indeed appear in the userAccounts collection. Win.
So in my head, I have an entity framework entities collection that I have mocked and primed with a user account. I would like to pass that into another class:
Authentication authentication =
UserAccount authenticatedUserAccount=authentication.Authenticate(userAccount.Username, userAccount.Password, Authentication.PasswordFormat.PlainText);
This uses the EF Model to return the user and authenticate their details.
This leaves the Question 2
: when this code runs, the UserAccounts collection within the mocked Entities object is (understandably) null. Where am I going wrong? Am I wrong to expect to be able to prime the mocked entities with a single object and be able to use that?
Any help appreciated, I've made time in my project to work on Mocking and I need to get to grips with this fast!