I have an Asp.Net Core 6 Web Api
application.
I have Integration tests made with WebApplicationFactory<Program>
as recommended here. I Inject it in the xUnit test class with IClassFixture<WebAdminTestApplicationFactory<Program>>
.
According to the documentation:
If your class owns a field or property, and its type implements IDisposable, the containing class itself should also implement IDisposable. A class that instantiates an IDisposable implementation and storing it as an instance member, is also responsible for its cleanup.
Then why MSDN examples for Integration tests don't dispose WebApplicationFactory
which they have as a field?
Shouldn't WebApplicationFactory
be disposed? Because it does implement IDisposable.
CodePudding user response:
In this case, your test class doesn't own WebApplicationFactory<Program>
: it's injected by xUnit, which handles calling Dispose
on the instance it creates and passes to you.
See Shared Context between Tests: Class Fixtures:
When using a class fixture, xUnit.net will ensure that the fixture instance will be created before any of the tests have run, and once all the tests have finished, it will clean up the fixture object by calling
Dispose
, if present.