{"id":18937515,"url":"https://github.com/verifytests/verify.aspnetcore","last_synced_at":"2025-04-15T18:32:16.455Z","repository":{"id":37885138,"uuid":"412306736","full_name":"VerifyTests/Verify.AspNetCore","owner":"VerifyTests","description":"Extends Verify to allow verification of AspNetCore bits.","archived":false,"fork":false,"pushed_at":"2024-04-30T02:35:03.000Z","size":1244,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-05-01T09:37:51.144Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/VerifyTests.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"license.txt","code_of_conduct":"code_of_conduct.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"SimonCropp"}},"created_at":"2021-10-01T03:05:07.000Z","updated_at":"2024-05-10T01:38:31.093Z","dependencies_parsed_at":"2023-09-26T08:36:18.747Z","dependency_job_id":"42b90ba3-39e5-4a65-940b-ccd21b04a530","html_url":"https://github.com/VerifyTests/Verify.AspNetCore","commit_stats":{"total_commits":189,"total_committers":3,"mean_commits":63.0,"dds":0.04761904761904767,"last_synced_commit":"ad2f819275c9f0ff86b8808c3066e7c0b63764e4"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VerifyTests%2FVerify.AspNetCore","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VerifyTests%2FVerify.AspNetCore/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VerifyTests%2FVerify.AspNetCore/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/VerifyTests%2FVerify.AspNetCore/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/VerifyTests","download_url":"https://codeload.github.com/VerifyTests/Verify.AspNetCore/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249129279,"owners_count":21217321,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T12:11:31.595Z","updated_at":"2025-04-15T18:32:11.447Z","avatar_url":"https://github.com/VerifyTests.png","language":"C#","funding_links":["https://github.com/sponsors/SimonCropp"],"categories":[],"sub_categories":[],"readme":"# \u003cimg src=\"/src/icon.png\" height=\"30px\"\u003e Verify.AspNetCore\n\n[![Discussions](https://img.shields.io/badge/Verify-Discussions-yellow?svg=true\u0026label=)](https://github.com/orgs/VerifyTests/discussions)\n[![Build status](https://ci.appveyor.com/api/projects/status/u4try12l1iimal2l?svg=true)](https://ci.appveyor.com/project/SimonCropp/verify-aspnetcore)\n[![NuGet Status](https://img.shields.io/nuget/v/Verify.AspNetCore.svg)](https://www.nuget.org/packages/Verify.AspNetCore/)\n\nExtends [Verify](https://github.com/VerifyTests/Verify) to allow verification of AspNetCore bits.\n\n**See [Milestones](../../milestones?state=closed) for release notes.**\n\n\n## NuGet package\n\nhttps://nuget.org/packages/Verify.AspNetCore/\n\n\n## Usage\n\nEnable VerifyAspNetCore once at assembly load time:\n\n\u003c!-- snippet: Enable --\u003e\n\u003ca id='snippet-Enable'\u003e\u003c/a\u003e\n```cs\n[ModuleInitializer]\npublic static void Initialize() =\u003e\n    VerifyAspNetCore.Initialize();\n```\n\u003csup\u003e\u003ca href='/src/Tests/ModuleInitializer.cs#L3-L9' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-Enable' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\n\n### Controller\n\nGiven the following controller:\n\n\u003c!-- snippet: MyController.cs --\u003e\n\u003ca id='snippet-MyController.cs'\u003e\u003c/a\u003e\n```cs\nusing Microsoft.AspNetCore.Mvc;\n\npublic class MyController :\n    Controller\n{\n    public ActionResult\u003cList\u003cDataItem\u003e\u003e Method(string input)\n    {\n        var headers = HttpContext.Response.Headers;\n        headers[\"headerKey\"] = \"headerValue\";\n        headers[\"receivedInput\"] = input;\n\n        var cookies = HttpContext.Response.Cookies;\n        cookies.Append(\"cookieKey\", \"cookieValue\");\n\n        var items = new List\u003cDataItem\u003e\n        {\n            new(\"Value1\"),\n            new(\"Value2\")\n        };\n        return new(items);\n    }\n\n    public class DataItem(string value)\n    {\n        public string Value { get; } = value;\n    }\n}\n```\n\u003csup\u003e\u003ca href='/src/Tests/Snippets/MyController.cs#L1-L27' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-MyController.cs' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\nThis test:\n\n\u003c!-- snippet: MyControllerTest --\u003e\n\u003ca id='snippet-MyControllerTest'\u003e\u003c/a\u003e\n```cs\n[Fact]\npublic Task Test()\n{\n    var context = new ControllerContext\n    {\n        HttpContext = new DefaultHttpContext()\n    };\n    var controller = new MyController\n    {\n        ControllerContext = context\n    };\n\n    var result = controller.Method(\"inputValue\");\n    return Verify(\n        new\n        {\n            result,\n            context\n        });\n}\n```\n\u003csup\u003e\u003ca href='/src/Tests/Snippets/MyControllerTests.cs#L5-L26' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-MyControllerTest' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\nWill result in the following verified file:\n\n\u003c!-- snippet: MyControllerTests.Test.verified.txt --\u003e\n\u003ca id='snippet-MyControllerTests.Test.verified.txt'\u003e\u003c/a\u003e\n```txt\n{\n  result: [\n    {\n      Value: Value1\n    },\n    {\n      Value: Value2\n    }\n  ],\n  context: {\n    HttpContext: {\n      Request: {},\n      IsAbortedRequested: false,\n      Response: {\n        StatusCode: OK,\n        Headers: {\n          headerKey: headerValue,\n          receivedInput: inputValue\n        },\n        Cookies: {\n          cookieKey: cookieValue\n        }\n      }\n    }\n  }\n}\n```\n\u003csup\u003e\u003ca href='/src/Tests/Snippets/MyControllerTests.Test.verified.txt#L1-L26' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-MyControllerTests.Test.verified.txt' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\n\n### Middleware\n\nGiven the following middleware:\n\n\u003c!-- snippet: MyMiddleware.cs --\u003e\n\u003ca id='snippet-MyMiddleware.cs'\u003e\u003c/a\u003e\n```cs\npublic class MyMiddleware(RequestDelegate next)\n{\n    public Task Invoke(HttpContext context)\n    {\n        context.Response.Headers[\"headerKey\"] = \"headerValue\";\n        return next(context);\n    }\n}\n```\n\u003csup\u003e\u003ca href='/src/Tests/Snippets/MyMiddleware.cs#L1-L8' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-MyMiddleware.cs' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\nThis test:\n\n\u003c!-- snippet: MyMiddlewareTest --\u003e\n\u003ca id='snippet-MyMiddlewareTest'\u003e\u003c/a\u003e\n```cs\n[Fact]\npublic async Task Test()\n{\n    var nextCalled = false;\n    var middleware = new MyMiddleware(\n        _ =\u003e\n        {\n            nextCalled = true;\n            return Task.CompletedTask;\n        });\n\n    var context = new DefaultHttpContext();\n    await middleware.Invoke(context);\n\n    await Verify(\n        new\n        {\n            context.Response,\n            nextCalled\n        });\n}\n```\n\u003csup\u003e\u003ca href='/src/Tests/Snippets/MyMiddlewareTests.cs#L3-L25' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-MyMiddlewareTest' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\nWill result in the following verified file:\n\n\u003c!-- snippet: MyMiddlewareTests.Test.verified.txt --\u003e\n\u003ca id='snippet-MyMiddlewareTests.Test.verified.txt'\u003e\u003c/a\u003e\n```txt\n{\n  Response: {\n    StatusCode: OK,\n    Headers: {\n      headerKey: headerValue\n    }\n  },\n  nextCalled: true\n}\n```\n\u003csup\u003e\u003ca href='/src/Tests/Snippets/MyMiddlewareTests.Test.verified.txt#L1-L9' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-MyMiddlewareTests.Test.verified.txt' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\n\n## Testing a web app with specific controller scenarios\n\n`UseSpecificControllers` extends `IMvcBuilder` to allow integration testing of a web app using a specific controller scenario.\n\n\u003c!-- snippet: TestController --\u003e\n\u003ca id='snippet-TestController'\u003e\u003c/a\u003e\n```cs\n[Fact]\npublic async Task ControllerIntegrationTest()\n{\n    var builder = WebApplication.CreateBuilder();\n\n    var controllers = builder.Services.AddControllers();\n    // custom extension\n    controllers.UseSpecificControllers(typeof(FooController));\n\n    await using var app = builder.Build();\n    app.MapControllers();\n\n    await app.StartAsync();\n\n    using var client = new HttpClient();\n    var result = client.GetStringAsync($\"{app.Urls.First()}/Foo\");\n\n    await Verify(result);\n}\n\n[ApiController]\n[Route(\"[controller]\")]\npublic class FooController :\n    ControllerBase\n{\n    [HttpGet]\n    public string Get() =\u003e\n        \"Foo\";\n}\n```\n\u003csup\u003e\u003ca href='/src/Tests/Tests.cs#L78-L110' title='Snippet source file'\u003esnippet source\u003c/a\u003e | \u003ca href='#snippet-TestController' title='Start of snippet'\u003eanchor\u003c/a\u003e\u003c/sup\u003e\n\u003c!-- endSnippet --\u003e\n\n\n## Icon\n\n[Spider](https://thenounproject.com/term/spider/904683/) designed by [marialuisa iborra](https://thenounproject.com/marialuisa.iborra/) from [The Noun Project](https://thenounproject.com).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fverifytests%2Fverify.aspnetcore","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fverifytests%2Fverify.aspnetcore","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fverifytests%2Fverify.aspnetcore/lists"}