Skip to content

Tests are useful. This repo will demonstrate how to add integration tests to an ASP.NET Core web application.

Notifications You must be signed in to change notification settings

winseros/DependenciesMockingDemoProject

Repository files navigation

Mock dependencies demo project

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.

Core features

Single button startup

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.

Real database in tests

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".

Entity framework SQL output

When running component tests, the test output shows the SQL generated by Entity Framework. That helps to build queries the right way.

SQL output in test window

Integration tests

The test engine runs the real application, allowing you to make real HTTP calls to it in a test.

Application log output

Integration tests output the application logs. That helps to ensure you've logged the right things the right way.

Application log output in test window

External HTTP mock

The project uses the Open Weather Map Api. The tests don't call the real service but use its mock instead.

Running the project

  1. 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 to CRLF. The setting can be disabled locally via git config --local core.autocrlf false.

  2. Run dotnet test

  3. Open the project in your favorite IDE

  4. Do the development

Managing Entity Framework migrations

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>

About

Tests are useful. This repo will demonstrate how to add integration tests to an ASP.NET Core web application.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published