Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/winseros/dependenciesmockingdemoproject
Tests are useful. This repo will demonstrate how to add integration tests to an ASP.NET Core web application.
https://github.com/winseros/dependenciesmockingdemoproject
asp-net-core aspnetcore integration-testing test-automation test-driven-development testing
Last synced: 4 days ago
JSON representation
Tests are useful. This repo will demonstrate how to add integration tests to an ASP.NET Core web application.
- Host: GitHub
- URL: https://github.com/winseros/dependenciesmockingdemoproject
- Owner: winseros
- Created: 2021-05-11T21:56:47.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2021-05-12T13:13:00.000Z (over 3 years ago)
- Last Synced: 2024-11-24T03:12:10.202Z (2 months ago)
- Topics: asp-net-core, aspnetcore, integration-testing, test-automation, test-driven-development, testing
- Language: C#
- Homepage:
- Size: 126 KB
- Stars: 2
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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](DependenciesMockingDemoProject.Test/Services/WeatherForecastServiceTest.cs). 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](doc/01-sql-in-tests.png "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](DependenciesMockingDemoProject.Int/Controllers/WeatherForecastControllerTest.cs).
### 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](doc/02-logs-in-tests.png "Application log output in test window")
### External HTTP mock
The project uses the [Open Weather Map Api](https://openweathermap.org/api). The tests don't call the real service but use its [mock](DependenciesMockingDemoProject.Int/Controllers/WeatherForecastControllerTest.cs#L56-L62) 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](./.mssql/) 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
3. 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.
```pwsh
$env:ENVIRONMENT="Development"
dotnet ef migrations add -o DataLayer\Migrations -p DependenciesMockingDemoProject.Web\DependenciesMockingDemoProject.Web.csproj
```