JonPSmith / EfCore.TestSupport

Tools for helping in unit testing applications that use Entity Framework Core
https://www.thereformedprogrammer.net/new-features-for-unit-testing-your-entity-framework-core-5-code/
Other
352 stars 53 forks source link

Suggestion: Extend EfCoreLogDecoder to decode CommandError as well as CommandExecuted #49

Closed MeltedFreddo closed 1 year ago

MeltedFreddo commented 2 years ago

A small suggestion for this handy little feature.

if (log.EventId.Name != RelationalEventId.CommandError.Name && log.EventId.Name != RelationalEventId.CommandExecuted.Name)

My use case was encountering a foreign key constraint error from sqlite when constructing a test database to run a unit test against - of course sqlite being unable to tell me which constraint I was violating. With a copy of the class with that small change I was able to pull the sql being executed and find what I was doing wrong. Maybe helpful in other situations too?

JonPSmith commented 2 years ago

Hi @MeltedFreddo,

I don't use the EfCoreLogDecoder since the EF Core 5 brought out the ToQueryString method because the EfCoreLogDecoder code isn't perfect. But I see why you are using the logging.

I'll leave this issue open and when I have another change I will update it.

PS. My solution if this problem is to comment out the SQLiite and replace with a SQL Server database to get a better database exception and then undo it afterwards, e.g.

[Fact]
public void TestAddRolesToDatabaseIfEmpty()
{
    //SETUP
    var options = SqliteInMemory.CreateOptions<AuthPermissionsDbContext>();
    //var options = this.CreateUniqueClassOptions<AuthPermissionsDbContext>();
    using var context = new AuthPermissionsDbContext(options);
    context.Database.EnsureCreated();
    //context.Database.EnsureClean();

   //... rest of test left out
JonPSmith commented 1 year ago

Hi @MeltedFreddo,

I have added your code to the 5.3.0 release, which supports .NET 7. I didn't check it so let me know if it works.

MeltedFreddo commented 1 year ago

That's great to hear, thank you very much!