question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do I unit test implementations of IEntityTypeConfiguration

See original GitHub issue

I hope this is the right place to ask this question. I am working on a class that extends IEntityTypeConfiguration and I want to unit test code that usually runs in DbContext.OnModelCreating.

For example:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
  modelBuilder.ApplyConfiguration(new MyCustomConfig("./some-seed-data.json"));
  modelBuilder.ApplyConfiguration(new MyOtherCustomConfig());
}

Should I be instantiating a ModelBuilder? I would prefer to not run a migration that creates files but that’s an acceptable side-effect.

Thanks

EF Core version: <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.1.0" /> Database Provider: <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.0" /> Operating system: Win 10 IDE: (e.g. Visual Studio 2017 15.4) VS Code

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
gforcegcommented, Aug 20, 2018

@ajcvickers, I am working on an abstract class that makes method calls in Configure() to perform configuration and to seed data from the file system (json, yaml, etc…).

public abstract class EntityConfiguration<TEntity> : IEntityTypeConfiguration<TEntity>
  where TEntity : class
  {
    public string FilePath { get; set; }
    
    public EntityConfiguration(string filePath = "")
    {
      this.FilePath = filePath;
    }

    public void Configure(EntityTypeBuilder<TEntity> builder)
    {
      DoConfiguration(builder);
      DoSeed(builder);
    }
    ...
}

I think what I’m currently trying to do is an integration test. I have a class ‘MyCustomConfig’ that inherits from EntityConfiguration. I want to assert that the following code will throw an exception when the json file doesn’t exist.

modelBuilder.ApplyConfiguration(new MyCustomConfig("./non-existent-file.json"));

I must wrap the call in modelBuilder.ApplyConfiguration otherwise, MyCustomConfig.Configure won’t be called. The only way I know how to do this is by running a migration from the dotnet cli and I think that would be more of a BDD test.

I would also like to be able to unit test the individual DoConfiguration(builder) and DoSeed(builder) functions but I don’t know how to instantiate EntityTypeBuilder.

0reactions
norcinocommented, Jan 18, 2023

You can unit test how your configuration is altering the builder using the following:

    var sut = new ScoringAssessmentOutcomeEntityTypeConfiguration();
    var entityType = new EntityType("MyType", typeof(MyType), new Model(), false, ConfigurationSource.Convention);            
     var builder = new EntityTypeBuilder<MyType>(entityType);
     
     sut.Configure(builder);

     var meta = builder.Metadata;
     var properties = builder.Metadata.GetDeclaredProperties();
     var viewName = builder.Metadata.GetViewName();
Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I unit test implementations of ...
I am working on a class that extends IEntityTypeConfiguration and I want to unit test code that usually runs in DbContext.OnModelCreating.
Read more >
Unit Test EntityFrameworkCore IEntityTypeConfiguration<T>
How do I unit test EntityFrameworkCore.IEntityTypeConfiguration? I want to make sure that my configuration is unit tested for several reasons:.
Read more >
Unit testing Fluent Validation rules against EF Core entity ...
I'll first set the scene: Show the EF Core Entity + Configuration + Fluent Validation we'll be working on. Next, I'll show the...
Read more >
Mocking Entity Framework when Unit Testing ASP.NET ...
Open the Test Explorer window, and notice the results of the tests. test results. Additional resources. Documentation. Implementing ...
Read more >
A Cleaner Way To Do Entity Configuration With EF Core
EF Core will then go and find all implementations of IEntityTypeConfiguration and use that as config for your data model. Perfect!
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found