I am trying to test that the business rule of not allowing to share the same space with a user twice. Below is the method being tested. The line having the issue is marked below.
public void ShareSpace(string spaceToShare,string emailToShareIt)
{
SharedSpace shareSpace = new SharedSpace();
shareSpace.InvitationCode = Guid.NewGuid().ToString("N");
shareSpace.DateSharedStarted = DateTime.Now;
shareSpace.Expiration = DateTime.Now.AddYears(DefaultShareExpirationInYears);
shareSpace.Active = true;
shareSpace.SpaceName = spaceToShare;
shareSpace.EmailAddress = emailToShareIt;
if (!this.MySpacesShared.IsLoaded)
this.MySpacesShared.Load(); //Here I am getting the exception further below.
if (this.MySpacesShared.Any(s => (s.EmailAddress == emailToShareIt)
& (s.SpaceName == spaceToShare)))
throw new InvalidOperationException("Cannot share the a space with a user twice.");
else
this.MySpacesShared.Add(shareSpace);
}
The TestMethod below:
[TestMethod]
public void Cannot_Share_SameSpace_with_same_userEmail_Twice()
{
account.ShareSpace("spaceName", "[email protected]");
try
{
account.ShareSpace("spaceName", "[email protected]");
Assert.Fail("Should throw exception when same space is shared with same user.");
}
catch (InvalidOperationException)
{ /* Expected */ }
Assert.AreEqual(1, account.MySpacesShared.Count);
Assert.AreSame(null, account.MySpacesShared.First().InvitedUser);
}
The error that I am getting on the test results:
Test method SpaceHelper.Tests.Controllers.SpaceControllerTest.Cannot_Share_SameSpace_with_same_userEmail_Twice threw exception: System.InvalidOperationException: The EntityCollection could not be loaded because it is not attached to an ObjectContext..
When I go step by step on the debugging mechanism this error comes up on the Load() event. I am pretty sure it has to do with the fact that I don't have a ADO.NET Entity Framework on my test scenario since I am using fake information here and is not hooked to my database.
I case anyone wants to see here is my initialization for that test:
[TestInitialize()]
public void MyTestInitialize()
{
user = new User()
{
Active = true,
Name = "Main User",
UserID = 1,
EmailAddress = "[email protected]",
OpenID = Guid.NewGuid().ToString()
};
account = new Account()
{
Key1 = "test1",
Key2 = "test2",
AccountName = "Brief Account Description",
ID = 1,
Owner = user
};
}