mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-02-16 10:42:08 +00:00
Merge pull request #118 from aaronpo97/117-feature-reqnroll-setup-for-bdd-testing
Set up Reqnroll for BDD testing
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -480,3 +480,5 @@ FodyWeavers.xsd
|
|||||||
*/data_source/other
|
*/data_source/other
|
||||||
.fake
|
.fake
|
||||||
.idea
|
.idea
|
||||||
|
|
||||||
|
*.feature.cs
|
||||||
37
src/Core/API/API.Specs/API.Specs.csproj
Normal file
37
src/Core/API/API.Specs/API.Specs.csproj
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net10.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<IsPackable>false</IsPackable>
|
||||||
|
<RootNamespace>API.Specs</RootNamespace>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
|
||||||
|
<PackageReference Include="xunit" Version="2.9.2" />
|
||||||
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
|
||||||
|
<PackageReference Include="FluentAssertions" Version="6.9.0" />
|
||||||
|
|
||||||
|
<!-- Reqnroll core, xUnit adapter and code-behind generator -->
|
||||||
|
<PackageReference Include="Reqnroll" Version="3.3.3" />
|
||||||
|
<PackageReference Include="Reqnroll.xUnit" Version="3.3.3" />
|
||||||
|
<PackageReference Include="Reqnroll.Tools.MsBuild.Generation" Version="3.3.3" PrivateAssets="all" />
|
||||||
|
|
||||||
|
<!-- ASP.NET Core integration testing -->
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.1" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<!-- Ensure feature files are included in the project -->
|
||||||
|
<None Include="Features\**\*.feature" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Using Include="Xunit" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\API.Core\API.Core.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
||||||
10
src/Core/API/API.Specs/Features/NotFound.feature
Normal file
10
src/Core/API/API.Specs/Features/NotFound.feature
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
Feature: NotFound API
|
||||||
|
As a client of the API
|
||||||
|
I want consistent 404 responses
|
||||||
|
So that consumers can handle missing routes
|
||||||
|
|
||||||
|
Scenario: GET error 404 returns NotFound message
|
||||||
|
Given the API is running
|
||||||
|
When I GET "/error/404"
|
||||||
|
Then the response status code should be 404
|
||||||
|
And the response JSON should have "message" equal "Route not found."
|
||||||
51
src/Core/API/API.Specs/Steps/ApiSteps.cs
Normal file
51
src/Core/API/API.Specs/Steps/ApiSteps.cs
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
using System.Net;
|
||||||
|
using System.Net.Http.Json;
|
||||||
|
using Reqnroll;
|
||||||
|
using FluentAssertions;
|
||||||
|
|
||||||
|
namespace API.Specs.Steps;
|
||||||
|
|
||||||
|
[Binding]
|
||||||
|
public class ApiSteps
|
||||||
|
{
|
||||||
|
private readonly TestApiFactory _factory;
|
||||||
|
private HttpClient? _client;
|
||||||
|
private HttpResponseMessage? _response;
|
||||||
|
|
||||||
|
public ApiSteps()
|
||||||
|
{
|
||||||
|
_factory = new TestApiFactory();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Given("the API is running")]
|
||||||
|
public void GivenTheApiIsRunning()
|
||||||
|
{
|
||||||
|
_client = _factory.CreateClient();
|
||||||
|
}
|
||||||
|
|
||||||
|
// No user service assumptions needed for 404 tests
|
||||||
|
|
||||||
|
[When("I GET {string}")]
|
||||||
|
public async Task WhenIGet(string path)
|
||||||
|
{
|
||||||
|
_client.Should().NotBeNull("API client must be initialized");
|
||||||
|
_response = await _client!.GetAsync(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Then("the response status code should be {int}")]
|
||||||
|
public void ThenStatusCodeShouldBe(int expected)
|
||||||
|
{
|
||||||
|
_response.Should().NotBeNull();
|
||||||
|
((int)_response!.StatusCode).Should().Be(expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Then("the response JSON should have {string} equal {string}")]
|
||||||
|
public async Task ThenResponseJsonShouldHaveFieldEqual(string field, string expected)
|
||||||
|
{
|
||||||
|
_response.Should().NotBeNull();
|
||||||
|
var dict = await _response!.Content.ReadFromJsonAsync<Dictionary<string, object>>();
|
||||||
|
dict.Should().NotBeNull();
|
||||||
|
dict!.TryGetValue(field, out var value).Should().BeTrue();
|
||||||
|
(value?.ToString()).Should().Be(expected);
|
||||||
|
}
|
||||||
|
}
|
||||||
15
src/Core/API/API.Specs/reqnroll.json
Normal file
15
src/Core/API/API.Specs/reqnroll.json
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"$schema": "https://raw.githubusercontent.com/reqnroll/Reqnroll/main/Reqnroll.Configuration/reqnroll.schema.json",
|
||||||
|
"language": {
|
||||||
|
"feature": "en-US"
|
||||||
|
},
|
||||||
|
"bindingCulture": {
|
||||||
|
"name": "en-US"
|
||||||
|
},
|
||||||
|
"trace": {
|
||||||
|
"level": "Verbose"
|
||||||
|
},
|
||||||
|
"runtime": {
|
||||||
|
"stopAtFirstError": false
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user