This project has moved and is read-only. For the latest updates, please go here.

EF5 Code First Unit Testing Issue

Nov 1, 2013 at 4:18 PM
Edited Nov 1, 2013 at 4:44 PM
Hi,

I am using EF5 code first in my project. In order to test my data access layer using Effort, I followed the tutorial to create a fake DbContext instance. The context is created and I am injecting this fake context instance to my data accessor class.

In my unit test, the first thing I do is to add some data but as soon as I call SaveChanges(), I get the following error:

System.NullReferenceException : Object reference not set to an instance of an object.
at Effort.Internal.DbManagement.DbExtensions.GetTable(Database database, String name)
at Effort.Internal.CommandActions.DbCommandActionHelper.GetTable(DbModificationCommandTree commandTree, DbContainer container)
at Effort.Internal.CommandActions.InsertCommandAction.ExecuteNonQuery(ActionContext context)
at Effort.Provider.EffortEntityCommand.ExecuteNonQuery()
at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary2 identifierValues, List1 generatedValues)
at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
at System.Data.Entity.DbContext.SaveChanges()

Code:
        [SetUp]
        public void SetUp()
        {
            DbConnection conn = DbConnectionFactory.CreateTransient();
            _context = new CmciContext(conn);
            _dataAccessor = new CmciHydrantFlowDataAccessor(_context);            
        }

        [TearDown]
        public void TearDown()
        {
            _context.Dispose();
        }

        [Test]
        public void TestGetFlowTestDataByRiskId_MatchFound()
        {
            const string riskId = "37PA99123456";

            _context.NeededFireFlowAreas.Add(
                new NeededFireFlowArea
                    {
                        NffAreaIdentifier = 1,
                        NffAreaName = "Test"
                    });

            _context.NeededFireFlows.Add(
                new NeededFireFlow
                    {
                        RiskId = riskId,
                        FlowTestDate = "12/1/2012",
                        StaticLocationHydrantFlowPsi = "Main St & Cross St",
                        NffAreaIdentifier = 1,
                        NffLocationIdentifier = 1
                    });

            _context.SaveChanges();

            IEnumerable<NeededFireFlow> neededFireFlows =  _dataAccessor.GetFlowTestDataByRiskId(riskId);
            Assert.IsTrue(neededFireFlows.Any());
        }
I have confirmed that my EF configuration is correct and that I can insert data using the actual context class.

Please help.

Thanks
Nov 2, 2013 at 5:27 PM
Edited Nov 2, 2013 at 5:28 PM
Hello,

I should add more diagnostics information to this exception. I added this to my TODO list. What happens if you add the following line in the SetUp?
_context.Database.CreateIfNotExists();
Marked as answer by manu79 on 11/5/2013 at 8:26 AM
Nov 5, 2013 at 4:26 PM
Thanks tamasflamich.
Adding this line in set up makes it work. Not sure why this line is not needed in the tutorial code but is required in my code.
Aug 21, 2014 at 1:04 PM
It seems that this code does not work:
if (!ctx.Database.Exists())
{
      ctx.Database.Create();
}
Any ideas?
Sep 8, 2014 at 7:50 PM
I will check.
Sep 8, 2014 at 7:51 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.