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

Add extensions to SqliteInMemory that return Options Builder as well … #33

Closed countincognito closed 4 years ago

countincognito commented 4 years ago

…as Options themselves.

JonPSmith commented 4 years ago

Hi @countincognito,

I too have sometimes needed that, so I will consider it. Can you tell me why you wanted a version which returns the options builder class?

I have a few tweaks that I want to do to this library, so I will add that to the list. I'm busy at the moment so I won't be working on this library straight away.

countincognito commented 4 years ago

Hello @JonPSmith

I was using SqliteInMemory as a provider substitute for some EFCore SqlServer classes (thank you for providing such a great library btw!), but unfortunately I hit a wall when I tried to test against DB calls that used transactions (since Sqlite throws an exception if it encounters them).

The only way I could find around this was to run the following expression for when the DbContext factory is added to the DI container:

optionsBuilder => optionsBuilder.ConfigureWarnings(x => x.Ignore(InMemoryEventId.TransactionIgnoredWarning))

With this in place the tests worked perfectly (way better than the InMemory provider). However, this call has to be made on the Options Builder itself before the Options are returned. These changes gave me access to the Options Builder so I could return the Options object after the necessary configurations were made. I thought they would be useful to share.

I hope this makes sense. Many thanks.

JonPSmith commented 4 years ago

Hi @countincognito,

That makes sense. As I say I will add that feature but it won't happen soon as I have other things on the go.

countincognito commented 4 years ago

@JonPSmith Thank you kindly. Much appreciated.

JonPSmith commented 4 years ago

Hi @countincognito,

I have just release a new version 3.2.0 with a method to add/override options at the DbContextOptionsBuilder<T>. I have added it to SqliteInMemory and the SQL Server versions. See SqliteInMemory docs.

I quite like this as its a) optional, and b) allows you to add new options or override options set by my code.

I'm going to close this pull request.