huysentruitw / entity-framework-core-mock

Easy Mock wrapper for mocking EFCore5 DbContext and DbSet using Moq or NSubstitute
MIT License
132 stars 24 forks source link

[Question] Dispose mock? #40

Closed lonix1 closed 2 years ago

lonix1 commented 2 years ago

I would typically dispose any real context (in memory, sqlite, whatever) that I create in a test.

I'm not sure how this library's mock is implemented. Does it need to be disposed, or it is mocks-all-the-way-down?

huysentruitw commented 2 years ago

The Moq version differs from the NSubstitute version. When using the Moq version (DbContextMock), you're essentially creating a new Mock where this library already does the heavy lifting to setup the mock (see https://github.com/cup-of-tea-dot-be/entity-framework-core-mock/blob/master/src/EntityFrameworkCoreMock.Moq/DbContextMock.cs#L20).

NSubstitute works a little bit different as there we're inheriting from DbContext itself, so this version is probably disposable.

This being said, this library mocks everything, so in either variants, there's not much to clean up.

lonix1 commented 2 years ago

Sorry, I didn't mention I'm using NSubstitute. Thanks for the info.

Given your comment "...inheriting from DbContext itself", I'll assume it's good to dispose just to be sure - I imagine 1,000s of tests which aren't disposed, could be a problem, maybe, possibly.

What I'm interested to know - the real "heavy" resource in a context, which should be disposed, is the DbConnection. How is that represented - is it mocked / subclassed / etc.?

huysentruitw commented 2 years ago

I don't have the resources to test this myself. If you have the time to test this, don't hesitate and report back. Meanwhile, I'll close this issue.