How do I unit test implementations of IEntityTypeConfiguration
See original GitHub issueI 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:
- Created 5 years ago
- Reactions:1
- Comments:10 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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…).
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.
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.
You can unit test how your configuration is altering the builder using the following: