Etl.Net icon indicating copy to clipboard operation
Etl.Net copied to clipboard

Documentation to Deal with dependency injection for Unit tests

Open paillave opened this issue 3 years ago • 1 comments

Discussed in https://github.com/paillave/Etl.Net/discussions/385

Originally posted by mlholt03 October 17, 2022 If I am access a database using this tool, and I wish to write unit tests for the code I am writing, how can I mock out the database access such that my unit test is not actually attempting to connect to a database? Is there some way to provide a SqlCommandValueProvider instance that simply returns a basic sample set of data without querying a database?

paillave avatar Oct 28 '22 11:10 paillave

About mocking and unit tests, the solutions are the same than the ones you use for regular NET development. At the moment, no there is no such things like SqlCommandValueProvider that returns data without querying the database. The ways you can do are the ones described in here: https://learn.microsoft.com/en-us/ef/core/testing/testing-without-the-database?source=recommendations

EF core In memory provider

https://learn.microsoft.com/en-us/ef/core/testing/testing-without-the-database?source=recommendations#in-memory-provider

Instead of injecting a DbContext that uses SQL Server, you inject a DBContext setup to work in memory instead

SQL Lite in memory

https://learn.microsoft.com/en-us/ef/core/testing/testing-without-the-database?source=recommendations#sqlite-in-memory

Instead of injecting a DbContext that uses SQL Server, you inject a DBContext setup to work with SQL lite by giving a connecting string that is setup to work in memory.

Repository

https://learn.microsoft.com/en-us/ef/core/testing/testing-without-the-database?source=recommendations#repository-pattern

In the same way you inject a DbConnection or a DbContext into ETL.NET, you can inject anything you want (Services or Repositories for instance).

Then, within your process, you can use what was injected by using ResolveAndSelect or ResolveAndDo operators.

If you have a business layer that you want to reuse within a stream, this is the way to go.

FYI: I change this conversation into an issue to remind me to improve the documentation regarding DI in processes

paillave avatar Oct 28 '22 11:10 paillave