Skip to content

Commit 072662b

Browse files
authored
Updated the Quick Start section of the README
1 parent a1517be commit 072662b

File tree

1 file changed

+50
-36
lines changed

1 file changed

+50
-36
lines changed

README.md

Lines changed: 50 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ Additionally, funds donated via [Patreon](https://www.patreon.com/ivaylokenov) (
111111
## Main Features
112112

113113
- **Built-in service mock resolver** - register your mocks once, use them everywhere.
114-
- **Controller tests** - unit or integration tests for both MVC or API scenarios.
114+
- **Full request setup** - excellent arrangement of fake request data.
115+
- **Detailed response assertions** - precise validation for all available result objects.
116+
- **Controller tests** - unit or integration tests for both MVC and API scenarios.
115117
- **View component tests** - validate your view logic as fast as possible.
116118
- **Route tests** - asserting route paths and model binding is as accessible as it can get.
117119
- **Pipeline tests** - from the web request to the returned response - the whole chain is validated.
@@ -123,37 +125,42 @@ Additionally, funds donated via [Patreon](https://www.patreon.com/ivaylokenov) (
123125

124126
## Quick Start
125127

128+
This quick start is for ASP.NET Core 3.1. For previous versions, check the [corresponding branches](https://github.com/ivaylokenov/MyTested.AspNetCore.Mvc/branches).
129+
126130
To add **MyTested.AspNetCore.Mvc** to your solution, you must follow these simple steps:
127131

128132
1. Create a test project.
129133
2. Reference your web application.
130134
3. Install **`MyTested.AspNetCore.Mvc.Universe`** (or just the [testing packages](#package-installation) you need) from [NuGet](https://www.nuget.org/packages/MyTested.AspNetCore.Mvc.Universe/).
131-
4. Open the test project's `.csproj` file.
132-
5. Change the project SDK to `Microsoft.NET.Sdk.Web`:
133-
134-
```xml
135-
<Project Sdk="Microsoft.NET.Sdk.Web">
136-
```
135+
4. Create a `testsettings.json` file at the root of the test project, set its `Copy to Output Directory` property to `Copy if newer`, and set your required application settings to fake ones:
137136

138-
6. Add a package reference to the web framework - `Microsoft.AspNetCore.App`:
139-
140-
```xml
141-
<PackageReference Include="Microsoft.AspNetCore.App" />
137+
```json
138+
{
139+
"ConnectionStrings": {
140+
"DefaultConnection": "My Fake Connection String"
141+
},
142+
"AllowedHosts": "*"
143+
}
142144
```
143145

144-
7. Your test project's `.csproj` file should be similar to this one:
146+
5. Your test project's `.csproj` file should be similar to this one:
145147

146148
```xml
147-
<Project Sdk="Microsoft.NET.Sdk.Web"> <!-- Changed project SDK -->
149+
<Project Sdk="Microsoft.NET.Sdk">
148150

149151
<PropertyGroup>
150-
<TargetFramework>netcoreapp2.2</TargetFramework>
152+
<TargetFramework>netcoreapp3.1</TargetFramework>
151153
</PropertyGroup>
152154

153155
<ItemGroup>
154-
<PackageReference Include="Microsoft.AspNetCore.App" /> <!-- Reference to the web framework -->
155-
<PackageReference Include="MyTested.AspNetCore.Mvc.Universe" Version="2.2.0" />
156-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
156+
<None Update="testsettings.json">
157+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
158+
</None>
159+
</ItemGroup>
160+
161+
<ItemGroup>
162+
<PackageReference Include="MyTested.AspNetCore.Mvc.Universe" Version="3.1.1" />
163+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
157164
<PackageReference Include="xunit" Version="2.4.1" /> <!-- Can be any testing framework -->
158165
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
159166
</ItemGroup>
@@ -165,7 +172,7 @@ To add **MyTested.AspNetCore.Mvc** to your solution, you must follow these simpl
165172
</Project>
166173
```
167174

168-
8. Create a `TestStartup` class at the root of the test project to register the dependency injection services, which will be used by all test cases in the assembly. A quick solution is to inherit from the web project's `Startup` class. By default **MyTested.AspNetCore.Mvc** replaces all ASP.NET Core services with ready to be used mocks. You only need to replace your own custom services with mocked ones by using the provided extension methods.
175+
6. Create a `TestStartup` class at the root of the test project to register the dependency injection services, which will be used by all test cases in the assembly. A quick solution is to inherit from the web project's `Startup` class. By default **MyTested.AspNetCore.Mvc** replaces all ASP.NET Core services with ready to be used mocks. You only need to replace your own custom services with mocked ones by using the provided extension methods.
169176

170177
```c#
171178
namespace MyApp.Tests
@@ -188,7 +195,7 @@ namespace MyApp.Tests
188195
}
189196
```
190197

191-
9. Create a test case by using the fluent API the library provides. You are given a static `MyMvc` class from which all assertions can be easily configured:
198+
7. Create a test case by using the fluent API the library provides. You are given a static `MyMvc` class from which all assertions can be easily configured:
192199

193200
```c#
194201
namespace MyApp.Tests.Controllers
@@ -203,10 +210,14 @@ namespace MyApp.Tests.Controllers
203210
[Fact]
204211
public void ReturnViewWhenCallingIndexAction()
205212
=> MyMvc
206-
.Controller<HomeController>()
213+
.Controller<HomeController>(instance => instance
214+
.WithUser("TestUser")
215+
.WithData(MyTestData.GetData()))
207216
.Calling(c => c.Index())
208217
.ShouldReturn()
209-
.Ok();
218+
.Ok(result => result
219+
.WithModelOfType<List<MyResponseModel>>()
220+
.Passing(model => model.Count == 10));
210221
}
211222
}
212223
```
@@ -348,7 +359,7 @@ MyController<MyMvcController>
348359

349360
### Route Tests
350361

351-
A route test validates the web application's routing configuration:
362+
A route test validates the web application's routing configuration, without executing the defined filters:
352363

353364
```c#
354365
// Tests a route for correct controller, action, and resolved route values.
@@ -358,7 +369,7 @@ MyRouting
358369
.To<MyController>(c => c.Action(1));
359370

360371
// Tests a route for correct controller, action, and resolved route values
361-
// with authenticated post request and submitted form.
372+
// with post request and submitted form.
362373
MyRouting
363374
.Configuration()
364375
.ShouldMap(request => request
@@ -368,9 +379,7 @@ MyRouting
368379
{
369380
Title = title,
370381
Content = content
371-
})
372-
.WithUser()
373-
.WithAntiForgeryToken())
382+
}))
374383
.To<MyController>(c => c.Action(new MyFormModel
375384
{
376385
Title = title,
@@ -380,14 +389,12 @@ MyRouting
380389
.ToValidModelState();
381390

382391
// Tests a route for correct controller, action, and resolved route values
383-
// with authenticated post request and JSON body.
392+
// with post request and JSON body.
384393
MyRouting
385394
.Configuration()
386395
.ShouldMap(request => request
387396
.WithLocation("/My/Action/1")
388397
.WithMethod(HttpMethod.Post)
389-
.WithUser()
390-
.WithAntiForgeryToken()
391398
.WithJsonBody(new
392399
{
393400
Integer = 1,
@@ -402,11 +409,11 @@ MyRouting
402409
.ToValidModelState();
403410
```
404411

405-
*Note: route tests execute action filters.*
412+
*Note: route tests do not execute action filters.*
406413

407414
### Pipeline tests
408415

409-
A pipeline test provides strongly-typed assertions for the MVC pipeline (from routing to action result):
416+
A pipeline test provides strongly-typed assertions for the MVC pipeline (from routing to action result, including the application filters):
410417

411418
```c#
412419
// Tests a route for correct route values,
@@ -416,26 +423,33 @@ A pipeline test provides strongly-typed assertions for the MVC pipeline (from ro
416423
// while resolving services from the `TestStartup` class.
417424
MyMvc
418425
.Pipeline()
419-
.ShouldMap("/My/Action/1")
426+
.ShouldMap(request => request
427+
.WithLocation("/My/Action/1")
428+
.WithMethod(HttpMethod.Post)
429+
.WithUser()
430+
.WithAntiForgeryToken()
431+
.WithJsonBody(new
432+
{
433+
Integer = 1,
434+
String = "Text"
435+
}))
420436
.To<MyController>(c => c.Action(1))
421437
.Which(controller => controller
422-
.WithUser()
423438
.WithData(MyDataProvider.GetMyModels()))
424439
.ShouldReturn()
425440
.Redirect(redirect => redirect
426441
.To<AnotherController>(c => c.AnotherAction()));
427442

428443
// Tests a route for correct route values,
429444
// and validates whether the controller action
430-
// with an authenticated user and the provided data
445+
// with the provided data
431446
// returns view result with a specific model,
432447
// while resolving services from the provided mocks.
433448
MyMvc
434449
.Pipeline()
435450
.ShouldMap("/My/Action/1")
436451
.To<MyController>(c => c.Action(1))
437452
.Which(controller => controller
438-
.WithUser()
439453
.WithData(MyDataProvider.GetMyModels())
440454
.WithDependencies(
441455
serviceMock,
@@ -665,7 +679,7 @@ using MyTested.AspNetCore.Mvc;
665679

666680
## Versioning
667681

668-
**MyTested.AspNetCore.Mvc** follows the ASP.NET Core MVC versions with which the testing framework is fully compatible. Specifically, the *major* and the *minor* versions will be incremented only when the MVC framework has a new official release. For example, version 2.2.\* of the testing framework is fully compatible with ASP.NET Core MVC 2.2.\*, version 1.1.\* is fully compatible with ASP.NET Core MVC 1.1.\*, version 1.0.15 is fully compatible with ASP.NET Core MVC 1.0.\*, and so on.
682+
**MyTested.AspNetCore.Mvc** follows the ASP.NET Core MVC versions with which the testing framework is fully compatible. Specifically, the *major* and the *minor* versions will be incremented only when the MVC framework has a new official release. For example, version 3.1.\* of the testing framework is fully compatible with ASP.NET Core MVC 3.1.\*, version 2.2.\* is fully compatible with ASP.NET Core MVC 2.2.\*, version 1.0.15 is fully compatible with ASP.NET Core MVC 1.0.\*, and so on.
669683

670684
The public interface of **MyTested.AspNetCore.Mvc** will not have any breaking changes when the version increases (unless entirely necessary).
671685

0 commit comments

Comments
 (0)