This demonstrates how you can mock the external dependencies of the application when doing unit and integration tests.
Note the code here to show you the idea, not to be accepted as the final truth.
When a developer gets the codebase, the only thing he has to do - is run docker-compose up
. That will run all the external dependencies the project needs.
You can cover the code that invokes the real database with tests. No "in-memory" stuff or other kinds of data-layer mocking.
Such tests are bigger than "unit" but still less than "integration", so I call them "component".
When running component tests, the test output shows the SQL generated by Entity Framework. That helps to build queries the right way.
The test engine runs the real application, allowing you to make real HTTP calls to it in a test.
Integration tests output the application logs. That helps to ensure you've logged the right things the right way.
The project uses the Open Weather Map Api. The tests don't call the real service but use its mock instead.
-
Run
docker-compose up
There is a caveat here. Git has a setting to convert line-endings to
CRLF
on its own disposal, and on windows this setting is often ON. The command will fail if *.sh files here get their line endings changed toCRLF
. The setting can be disabled locally viagit config --local core.autocrlf false
. -
Run
dotnet test
-
Open the project in your favorite IDE
-
Do the development
To manage the Entity Framework migrations, you need to set the ENVIRONMENT
system variable to Development
before. Otherwise, the migration management will fail.
$env:ENVIRONMENT="Development"
dotnet ef migrations add -o DataLayer\Migrations -p DependenciesMockingDemoProject.Web\DependenciesMockingDemoProject.Web.csproj <MigrationName>