You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+50-36Lines changed: 50 additions & 36 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -111,7 +111,9 @@ Additionally, funds donated via [Patreon](https://www.patreon.com/ivaylokenov) (
111
111
## Main Features
112
112
113
113
-**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.
115
117
-**View component tests** - validate your view logic as fast as possible.
116
118
-**Route tests** - asserting route paths and model binding is as accessible as it can get.
117
119
-**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) (
123
125
124
126
## Quick Start
125
127
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
+
126
130
To add **MyTested.AspNetCore.Mvc** to your solution, you must follow these simple steps:
127
131
128
132
1. Create a test project.
129
133
2. Reference your web application.
130
134
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
-
<ProjectSdk="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:
137
136
138
-
6. Add a package reference to the web framework - `Microsoft.AspNetCore.App`:
@@ -165,7 +172,7 @@ To add **MyTested.AspNetCore.Mvc** to your solution, you must follow these simpl
165
172
</Project>
166
173
```
167
174
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.
169
176
170
177
```c#
171
178
namespaceMyApp.Tests
@@ -188,7 +195,7 @@ namespace MyApp.Tests
188
195
}
189
196
```
190
197
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:
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:
352
363
353
364
```c#
354
365
// Tests a route for correct controller, action, and resolved route values.
@@ -358,7 +369,7 @@ MyRouting
358
369
.To<MyController>(c=>c.Action(1));
359
370
360
371
// 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.
362
373
MyRouting
363
374
.Configuration()
364
375
.ShouldMap(request=>request
@@ -368,9 +379,7 @@ MyRouting
368
379
{
369
380
Title=title,
370
381
Content=content
371
-
})
372
-
.WithUser()
373
-
.WithAntiForgeryToken())
382
+
}))
374
383
.To<MyController>(c=>c.Action(newMyFormModel
375
384
{
376
385
Title=title,
@@ -380,14 +389,12 @@ MyRouting
380
389
.ToValidModelState();
381
390
382
391
// 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.
384
393
MyRouting
385
394
.Configuration()
386
395
.ShouldMap(request=>request
387
396
.WithLocation("/My/Action/1")
388
397
.WithMethod(HttpMethod.Post)
389
-
.WithUser()
390
-
.WithAntiForgeryToken()
391
398
.WithJsonBody(new
392
399
{
393
400
Integer=1,
@@ -402,11 +409,11 @@ MyRouting
402
409
.ToValidModelState();
403
410
```
404
411
405
-
*Note: route tests execute action filters.*
412
+
*Note: route tests do not execute action filters.*
406
413
407
414
### Pipeline tests
408
415
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):
410
417
411
418
```c#
412
419
// Tests a route for correct route values,
@@ -416,26 +423,33 @@ A pipeline test provides strongly-typed assertions for the MVC pipeline (from ro
416
423
// while resolving services from the `TestStartup` class.
417
424
MyMvc
418
425
.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
+
}))
420
436
.To<MyController>(c=>c.Action(1))
421
437
.Which(controller=>controller
422
-
.WithUser()
423
438
.WithData(MyDataProvider.GetMyModels()))
424
439
.ShouldReturn()
425
440
.Redirect(redirect=>redirect
426
441
.To<AnotherController>(c=>c.AnotherAction()));
427
442
428
443
// Tests a route for correct route values,
429
444
// and validates whether the controller action
430
-
// with an authenticated user and the provided data
445
+
// with the provided data
431
446
// returns view result with a specific model,
432
447
// while resolving services from the provided mocks.
433
448
MyMvc
434
449
.Pipeline()
435
450
.ShouldMap("/My/Action/1")
436
451
.To<MyController>(c=>c.Action(1))
437
452
.Which(controller=>controller
438
-
.WithUser()
439
453
.WithData(MyDataProvider.GetMyModels())
440
454
.WithDependencies(
441
455
serviceMock,
@@ -665,7 +679,7 @@ using MyTested.AspNetCore.Mvc;
665
679
666
680
## Versioning
667
681
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.
669
683
670
684
The public interface of **MyTested.AspNetCore.Mvc** will not have any breaking changes when the version increases (unless entirely necessary).
0 commit comments