单元/集成测试EF与Effort - 找不到Connectionstring

时间:2017-03-16 06:58:49

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

我最近开始使用内存数据库Effort对我的实体框架实现进行单元测试。

一切正常,除非我尝试使用CSV数据加载器将CSV文件中的数据加载到数据库中。

关于如何配置csvdataloader的文档似乎很少/不足。

这是我编写的NUnit 3.0单元测试之一:

    [Test]
    public void GetBudgetIncomeItems_MultipleValidItems_ReturnList()
    {
        //Prepare and load data
        var dataLoader = new CsvDataLoader(@"<DirectoryOfUnitTestProject>\Data");
        var entityConnection = Effort.EntityConnectionFactory.CreateTransient("name=<NameOfMyCSVFileWithoutCSVExtension/DatabaseName>", dataLoader);
        _serviceContext = new ServiceContext(entityConnection);

        //Execute test
        var items = _query.GetBudgetIncomeItems(new Application {Id = 3});

        //Check results
        //Some assertions
    }

线路上抛出异常:

Effort.EntityConnectionFactory.CreateTransient("name=<NameOfMyCSVFileWithoutCSVExtension/DatabaseName>", dataLoader);

例外细节:

  

System.ArgumentException未被用户代码
处理   HResult = -2147024809 Message =找不到Connectionstring参数   name:entityConnectionString ParamName = entityConnectionString
  Source = Effort StackTrace:          在Effort.EntityConnectionFactory.GetFullEntityConnectionString(String   entityConnectionString)          在Effort.EntityConnectionFactory.GetEffortCompatibleMetadataWorkspace(String&amp;   entityConnectionString)          在Effort.EntityConnectionFactory.CreateTransient(String entityConnectionString,IDataLoader dataLoader)          在Infrastructure.EF6.UnitTests.PacsQueryTests`1.GetBudgetIncomeItems_MultipleValidItems_ReturnList()   在   \ PacsQueryTests.cs:行   36 InnerException:

任何帮助将不胜感激......

更新

根据以下@Evk的评论,当首先使用Code时,您必须使用DbConnection和DbConnectionFactory类,而不是EntityConnection和EntityConnectionFactory类。

我的更新代码不会引发任何错误,如下所示:

        [Test]
        public void GetBudgetIncomeItems_MultipleValidItems_ReturnList()
        {
            //Prepare and load data
            Effort.DataLoaders.IDataLoader csvDataLoader = new Effort.DataLoaders.CsvDataLoader(@"<DirectoryOfUnitTestProject>\Data");
            var dataLoader = new Effort.DataLoaders.CachingDataLoader(csvDataLoader,false);
            DbConnection dbConnection = Effort.DbConnectionFactory.CreateTransient(dataLoader);
            _serviceContext = new ServiceContext(dbConnection);

            //Execute test
            var items = _query.GetBudgetIncomeItems(new Application {Id = 3});

            //Check results
            //Some assertions
        }

1 个答案:

答案 0 :(得分:0)

我知道我参加这个聚会很晚,但是他们的职位似乎可以回答这个问题。我花了半天时间寻找它。希望将其发布在这里能够传播这个词。

https://github.com/zzzprojects/EntityFramework-Effort/issues/99

信用归上述原始帖子的作者。

EntityFrameworkEffortManager.ContextFactory = context => new MyContext();
var dataLoader = new EntityDataLoader();
DbConnection dbConnection = DbConnectionFactory.CreateTransient(dataLoader);
var effortContext = new MyContext(dbConnection);