'Effort.EntityConnectionFactory'的类型初始值设定项在MOQ单元测试

时间:2018-03-16 10:26:28

标签: c# entity-framework unit-testing moq effort

我正在使用MOQ和Effort来模拟数据库以进行单元测试。我已经努力创建内存数据库并使用运行时数据填充表并使用MOQ模拟存储过程。当我立即运行所有测试时,我的努力相关单元测试失败,当我重新运行失败的测试然后一切都是绿色。当我尝试调试所有测试时,我得到了异常。

这里我粘贴了我的DatabaseContext代码。

         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代码段:

       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();
    } 

单元测试方法内部
            //法案             ActivitiesTestData.AddTestData(_context);             var result = _activityRepository.ErrorDetailbyErrorId(errorId,             timezoneoffset的);

0 个答案:

没有答案