https://github.com/verifytests/verify.aspnetcore
Extends Verify to allow verification of AspNetCore bits.
https://github.com/verifytests/verify.aspnetcore
Last synced: about 1 year ago
JSON representation
Extends Verify to allow verification of AspNetCore bits.
- Host: GitHub
- URL: https://github.com/verifytests/verify.aspnetcore
- Owner: VerifyTests
- License: mit
- Created: 2021-10-01T03:05:07.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-04-30T02:35:03.000Z (about 2 years ago)
- Last Synced: 2024-05-01T09:37:51.144Z (about 2 years ago)
- Language: C#
- Homepage:
- Size: 1.19 MB
- Stars: 4
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- Funding: .github/FUNDING.yml
- License: license.txt
- Code of conduct: code_of_conduct.md
Awesome Lists containing this project
README
#
Verify.AspNetCore
[](https://github.com/orgs/VerifyTests/discussions)
[](https://ci.appveyor.com/project/SimonCropp/verify-aspnetcore)
[](https://www.nuget.org/packages/Verify.AspNetCore/)
Extends [Verify](https://github.com/VerifyTests/Verify) to allow verification of AspNetCore bits.
**See [Milestones](../../milestones?state=closed) for release notes.**
## NuGet package
https://nuget.org/packages/Verify.AspNetCore/
## Usage
Enable VerifyAspNetCore once at assembly load time:
```cs
[ModuleInitializer]
public static void Initialize() =>
VerifyAspNetCore.Initialize();
```
snippet source | anchor
### Controller
Given the following controller:
```cs
using Microsoft.AspNetCore.Mvc;
public class MyController :
Controller
{
public ActionResult> Method(string input)
{
var headers = HttpContext.Response.Headers;
headers["headerKey"] = "headerValue";
headers["receivedInput"] = input;
var cookies = HttpContext.Response.Cookies;
cookies.Append("cookieKey", "cookieValue");
var items = new List
{
new("Value1"),
new("Value2")
};
return new(items);
}
public class DataItem(string value)
{
public string Value { get; } = value;
}
}
```
snippet source | anchor
This test:
```cs
[Fact]
public Task Test()
{
var context = new ControllerContext
{
HttpContext = new DefaultHttpContext()
};
var controller = new MyController
{
ControllerContext = context
};
var result = controller.Method("inputValue");
return Verify(
new
{
result,
context
});
}
```
snippet source | anchor
Will result in the following verified file:
```txt
{
result: [
{
Value: Value1
},
{
Value: Value2
}
],
context: {
HttpContext: {
Request: {},
IsAbortedRequested: false,
Response: {
StatusCode: OK,
Headers: {
headerKey: headerValue,
receivedInput: inputValue
},
Cookies: {
cookieKey: cookieValue
}
}
}
}
}
```
snippet source | anchor
### Middleware
Given the following middleware:
```cs
public class MyMiddleware(RequestDelegate next)
{
public Task Invoke(HttpContext context)
{
context.Response.Headers["headerKey"] = "headerValue";
return next(context);
}
}
```
snippet source | anchor
This test:
```cs
[Fact]
public async Task Test()
{
var nextCalled = false;
var middleware = new MyMiddleware(
_ =>
{
nextCalled = true;
return Task.CompletedTask;
});
var context = new DefaultHttpContext();
await middleware.Invoke(context);
await Verify(
new
{
context.Response,
nextCalled
});
}
```
snippet source | anchor
Will result in the following verified file:
```txt
{
Response: {
StatusCode: OK,
Headers: {
headerKey: headerValue
}
},
nextCalled: true
}
```
snippet source | anchor
## Testing a web app with specific controller scenarios
`UseSpecificControllers` extends `IMvcBuilder` to allow integration testing of a web app using a specific controller scenario.
```cs
[Fact]
public async Task ControllerIntegrationTest()
{
var builder = WebApplication.CreateBuilder();
var controllers = builder.Services.AddControllers();
// custom extension
controllers.UseSpecificControllers(typeof(FooController));
await using var app = builder.Build();
app.MapControllers();
await app.StartAsync();
using var client = new HttpClient();
var result = client.GetStringAsync($"{app.Urls.First()}/Foo");
await Verify(result);
}
[ApiController]
[Route("[controller]")]
public class FooController :
ControllerBase
{
[HttpGet]
public string Get() =>
"Foo";
}
```
snippet source | anchor
## Icon
[Spider](https://thenounproject.com/term/spider/904683/) designed by [marialuisa iborra](https://thenounproject.com/marialuisa.iborra/) from [The Noun Project](https://thenounproject.com).