ASP.NET Effort CsvLoader does not return data

279 Views Asked by At

I'm using Entity Framework 6 with Code First and I want to mock my DbContext for unit testing. Because I need lots of test data for the unit tests, I wanted to load them from a csv-file using Entity Framework Effort (https://entityframework-effort.net/).

I don't get any errors, but I also don't get any data from the csv-file. All I get is a DbContext (the FapContext in the code) with no data.

Do you have any suggestions to solve my problem?

Unit Test with NUnit:

    private FapContext context;
    private IUnitOfWork unitOfWork;
    private IDataLoader csvLoader;
    private IDataLoader dataLoader;

    [SetUp]
    public void SetUp()
    {
        this.csvLoader = new CsvDataLoader(@"C:\path\TestData\CSV-TestData");
        this.dataLoader = new CachingDataLoader(this.csvLoader, false);
        DbConnection connection = DbConnectionFactory.CreateTransient(this.dataLoader);

        try
        {
            this.context = new FapContext(connection);
            this.context.Database.CreateIfNotExists();
        }
        catch (Exception e)
        {
            Debug.Print(e.GetType().FullName);
            throw;
        }

        this.unitOfWork = new UnitOfWork(this.context);
    }

    [Test]
    public void ShouldReturnData()
    {
        // Arrange
        List<State> states = this.context.States.ToList();

        // Act

        // Assert
        Assert.IsNotNull(states);           // passes
        Assert.IsTrue(states.Count > 0);    // don't pass
    }

CSV-File:

ID,StateGroupID,Name,Description,CreatedAt,ChangedAt,RowVersion
"187968CE-0489-4A57-81F4-0BECDF9CC005","5FBAD179-68F5-46D2-A0A6-82516A3F2E4D","APPROVED","Approved material",,,
"222E365D-A8D7-4DFF-8807-18EC85481FD8","AED80358-D2A3-4CC9-A7E5-EA1C8931C27A","Running",,,,
"8433F457-31F1-40D4-AF78-2058415407F7","28E828D7-6B5B-9BEC-928D-101DEB38B4C5","Pending",,,,
1

There are 1 best solutions below

0
Brian On

I know this question is pretty old, but I just recently ran into this while adding tests to an old EF6 project.

OP didn't document their CSV filename. I'm going to guess your filename doesn't match your DBContext's property names exactly. You need to match the property name, not the class name. I'm assuming your pluralization differs from model/table to property. Example: I'm guessing your CSV filename is something like State.csv while your DbContext property name is States.