The type initializer for 'Effort.EntityConnectionFactory' threw an exception in MOQ unit test

338 Views Asked by At

I am using MOQ and Effort to mock database for unit tests. I have used effort to create in-memory database and filling tables with runtime data and using MOQ for mocking stored procedures. When i run all the tests at once then my effort related unit tests failed and when i re-run failed tests then everything in green. I get above exception when i try to debug all tests.

Here i am pasting my DatabaseContext Code.

         public DAL.IRapidDevOpsDBContext CreateContext()
    {
        base.CreateConnection();
        var context = new RapidDevOpsEntities(_connection);
        return context;
    }

     protected EntityConnection _connection;
    protected void CreateConnection()
    {
        if (_connection == null)
        {
            _connection = 

     EntityConnectionFactory.CreateTransient("name=MycontextName");
        }
    }

MOQ code snippet :

       var mockStoredProcedure_Result = new 
         Mock<ObjectResult<getActivities_Result>>();

        var valueEnumerator = new getActivities_Result[] { 
       GetActivityDetailsList() }.ToList().GetEnumerator();

        mockStoredProcedure_Result.Setup(x => 
      x.GetEnumerator()).Returns(valueEnumerator);

        string expectedJson = "
    [{ActivityID:111,ActivityName:Test_Act,ActivityDetail:Test_Details}]";

        var dcc = new Mock<RapidDevOpsEntities>();


        dcc.Setup(x => x.getActivities(fromDate, toDate, "", 
        14158)).Returns(mockStoredProcedure_Result.Object);

        _activityRepository = new ActivityController(dcc.Object);

Effort code snippet:

      [TestInitialize]
    public void SetupTest()
    {

        // create the test strategy.  This will initialise a new database
        _effortDatabaseStrategy = CreateTestStrategy();

        // add test data to the database instance
        _context = _effortDatabaseStrategy.CreateContext();

        // initialise the repositories we are testing
        _activityRepository = new ActivityController(_context);
    }

    protected IDatabaseContext CreateTestStrategy()
    {
        return new DatabaseContext();
    } 

Inside the unit test method
//Act ActivitiesTestData.AddTestData(_context); var result = _activityRepository.ErrorDetailbyErrorId(errorId, timeZoneOffset);

0

There are 0 best solutions below