Skip to content

Commit bab8066

Browse files
committed
Updated README to include pipeline test examples (#282)
1 parent a50efdc commit bab8066

File tree

2 files changed

+121
-75
lines changed

2 files changed

+121
-75
lines changed

README.md

Lines changed: 121 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -167,49 +167,6 @@ It is **strongly advised** to read the [tutorial](http://docs.mytestedasp.net/tu
167167

168168
You can also check out the [provided samples](https://github.com/ivaylokenov/MyTested.AspNetCore.Mvc/tree/version-2.2/samples) for real-life ASP.NET Core MVC application testing.
169169

170-
## Package Installation
171-
172-
You can install this library using [NuGet](https://www.nuget.org/packages/MyTested.AspNetCore.Mvc.Universe) into your test project (or reference it directly in your `.csproj` file). Currently **MyTested.AspNetCore.Mvc** is fully compatible with ASP.NET Core MVC 2.2.0 and all older versions available on the official NuGet feed.
173-
174-
```powershell
175-
Install-Package MyTested.AspNetCore.Mvc.Universe
176-
```
177-
178-
This package will include all available assertion methods in your test project, including ones for authentication, database, session, caching and more. If you want only the MVC related features, install `MyTested.AspNetCore.Mvc`. If you want to use the completely **FREE** and **UNLIMITED** version of the library, install only `MyTested.AspNetCore.Mvc.Lite` and no other package. Additionally, if you prefer, you can be more specific by including only some of the packages:
179-
180-
- `MyTested.AspNetCore.Mvc.Configuration` - Contains setup and assertion methods for configurations
181-
- `MyTested.AspNetCore.Mvc.Controllers` - Contains setup and assertion methods for controllers
182-
- `MyTested.AspNetCore.Mvc.Controllers.Attributes` - Contains setup and assertion methods for controller attributes
183-
- `MyTested.AspNetCore.Mvc.Controllers.ActionResults` - Contains setup and assertion methods for controller API action results
184-
- `MyTested.AspNetCore.Mvc.Controllers.Views` - Contains setup and assertion methods for controller view features
185-
- `MyTested.AspNetCore.Mvc.Controllers.Views.ActionResults` - Contains setup and assertion methods for controller view action results
186-
- `MyTested.AspNetCore.Mvc.Models` - Contains setup and assertion methods for response and view models
187-
- `MyTested.AspNetCore.Mvc.Routing` - Contains setup and assertion methods for routes
188-
- `MyTested.AspNetCore.Mvc.Core` - Contains setup and assertion methods for MVC core features
189-
- `MyTested.AspNetCore.Mvc.TempData` - Contains setup and assertion methods for `ITempDataDictionary`
190-
- `MyTested.AspNetCore.Mvc.ViewData` - Contains assertion methods for `ViewDataDictionary` and dynamic `ViewBag`
191-
- `MyTested.AspNetCore.Mvc.ViewComponents` - Contains setup and assertion methods for view components
192-
- `MyTested.AspNetCore.Mvc.ViewComponents.Attributes` - Contains setup and assertion methods for view component attributes
193-
- `MyTested.AspNetCore.Mvc.ViewComponents.Results` - Contains setup and assertion methods for view component results
194-
- `MyTested.AspNetCore.Mvc.ViewFeatures` - Contains setup and assertion methods for MVC view features
195-
- `MyTested.AspNetCore.Mvc.Http` - Contains setup and assertion methods for HTTP context, request and response
196-
- `MyTested.AspNetCore.Mvc.Authentication` - Contains setup methods for `ClaimsPrincipal`
197-
- `MyTested.AspNetCore.Mvc.ModelState` - Contains setup and assertion methods for `ModelStateDictionary` validations
198-
- `MyTested.AspNetCore.Mvc.DataAnnotations` - Contains setup and assertion methods for data annotation validations
199-
- `MyTested.AspNetCore.Mvc.EntityFrameworkCore` - Contains setup and assertion methods for `DbContext`
200-
- `MyTested.AspNetCore.Mvc.DependencyInjection` - Contains setup methods for dependency injection services
201-
- `MyTested.AspNetCore.Mvc.Caching` - Contains setup and assertion methods for `IMemoryCache`
202-
- `MyTested.AspNetCore.Mvc.Session` - Contains setup and assertion methods for `ISession`
203-
- `MyTested.AspNetCore.Mvc.Options` - Contains setup and assertion methods for `IOptions`
204-
- `MyTested.AspNetCore.Mvc.Helpers` - Contains additional helper methods for easier assertions
205-
- `MyTested.AspNetCore.Mvc.Lite` - Completely **FREE** and **UNLIMITED** version of the library. It should not be used in combination with any other package. Includes `Controllers`, `Controllers.Views` and `ViewComponents`.
206-
207-
After the downloading is complete, just add `using MyTested.AspNetCore.Mvc;` to your source code and you are ready to test in the most elegant and developer friendly way.
208-
209-
```c#
210-
using MyTested.AspNetCore.Mvc;
211-
```
212-
213170
## Test Examples
214171

215172
Here are some examples of how **powerful** the fluent testing API actually is!
@@ -227,9 +184,9 @@ A controller integration test uses the globally registered services from the `Te
227184
// and tests for added by the action view bag entry,
228185
// and tests for view result and model with specific assertions.
229186
MyController<MyMvcController>
230-
.Instance()
231-
.WithUser(user => user
232-
.WithUsername("MyUserName"))
187+
.Instance(instance => instance
188+
.WithUser(user => user
189+
.WithUsername("MyUserName")))
233190
.Calling(c => c.MyAction(myRequestModel))
234191
.ShouldHave()
235192
.ValidModelState()
@@ -254,14 +211,14 @@ MyController<MyMvcController>
254211
// and tests for added by the action cache entry,
255212
// and tests for view result with specific model type.
256213
MyController<MyMvcController>
257-
.Instance()
258-
.WithOptions(options => options
259-
.For<MyAppSettings>(settings => settings.Cache = true))
260-
.WithSession(session => session
261-
.WithEntry("MySession", "MySessionValue"))
262-
.WithData(data => data
263-
.WithEntities(entities => entities
264-
.AddRange(MyDataProvider.GetMyModels())))
214+
.Instance(instance => instance
215+
.WithOptions(options => options
216+
.For<MyAppSettings>(settings => settings.Cache = true))
217+
.WithSession(session => session
218+
.WithEntry("MySession", "MySessionValue"))
219+
.WithData(data => data
220+
.WithEntities(entities => entities
221+
.AddRange(MyDataProvider.GetMyModels()))))
265222
.Calling(c => c.MyAction())
266223
.ShouldHave()
267224
.MemoryCache(cache => cache
@@ -316,22 +273,22 @@ A controller unit test uses service mocks explicitly provided in each separate a
316273
// Instantiates controller with the provided service mocks,
317274
// and tests for view result.
318275
MyController<MyMvcController>
319-
.Instance()
320-
.WithDependencies(
321-
serviceMock,
322-
anotherServiceMock,
323-
From.Services<IYetAnotherService>()) // Provides a global service.
276+
.Instance(instance => instance
277+
.WithDependencies(
278+
serviceMock,
279+
anotherServiceMock,
280+
From.Services<IYetAnotherService>())) // Provides a global service.
324281
.Calling(c => c.MyAction())
325282
.ShouldReturn()
326283
.View();
327284

328285
// Instantiates controller with the provided service mocks,
329286
// and tests for view result.
330287
MyController<MyMvcController>
331-
.Instance()
332-
.WithDependencies(dependencies => dependencies
333-
.With<IService>(serviceMock)
334-
.WithNo<IAnotherService>()) // Provides null for IAnotherService.
288+
.Instance(instance => instance
289+
.WithDependencies(dependencies => dependencies
290+
.With<IService>(serviceMock)
291+
.WithNo<IAnotherService>())) // Provides null for IAnotherService.
335292
.Calling(c => c.MyAction(From.Services<IYetAnotherService>())) // Provides a global service.
336293
.ShouldReturn()
337294
.View();
@@ -393,6 +350,53 @@ MyRouting
393350
.ToValidModelState();
394351
```
395352

353+
*Note: route tests execute action filters.*
354+
355+
### Pipeline tests
356+
357+
A pipeline test provides strongly-typed assertions for the MVC pipeline (from routing to action result):
358+
359+
```c#
360+
// Tests a route for correct route values,
361+
// and validates whether the controller action
362+
// with an authenticated user and the provided data
363+
// returns redirect result to a specific action,
364+
// while resolving services from the `TestStartup` class.
365+
MyMvc
366+
.Pipeline()
367+
.ShouldMap("/My/Action/1")
368+
.To<MyController>(c => c.Action(1))
369+
.Which(controller => controller
370+
.WithUser()
371+
.WithData(MyDataProvider.GetMyModels()))
372+
.ShouldReturn()
373+
.Redirect(redirect => redirect
374+
.To<AnotherController>(c => c.AnotherAction()));
375+
376+
// Tests a route for correct route values,
377+
// and validates whether the controller action
378+
// with an authenticated user and the provided data
379+
// returns view result with a specific model,
380+
// while resolving services from the provided mocks.
381+
MyMvc
382+
.Pipeline()
383+
.ShouldMap("/My/Action/1")
384+
.To<MyController>(c => c.Action(1))
385+
.Which(controller => controller
386+
.WithUser()
387+
.WithData(MyDataProvider.GetMyModels())
388+
.WithDependencies(
389+
serviceMock,
390+
anotherServiceMock))
391+
.ShouldReturn()
392+
.View(result => result
393+
.WithModelOfType<MyResponseModel>());
394+
```
395+
396+
*Note: pipeline tests execute action filters.*
397+
*Note: pipeline tests does not run the server middleware configuration.*
398+
*Note: pipeline tests are available from version 3.0.0 onwards.*
399+
396400
### Attribute Declaration Tests
397401

398402
An attribute declaration test validates controller and action attribute declarations:
@@ -423,12 +427,12 @@ All applicable methods are available on the view component testing API too:
423427
```c#
424428
// View component integration test.
425429
MyViewComponent<MyMvcController>
426-
.Instance()
427-
.WithSession(session => session
428-
.WithEntry("MySession", "MySessionValue"))
429-
.WithData(data => data
430-
.WithEntities(entities => entities
431-
.AddRange(MyDataProvider.GetMyModels())))
430+
.Instance(instance => instance
431+
.WithSession(session => session
432+
.WithEntry("MySession", "MySessionValue"))
433+
.WithData(data => data
434+
.WithEntities(entities => entities
435+
.AddRange(MyDataProvider.GetMyModels()))))
432436
.InvokedWith(c => c.InvokeAsync(1))
433437
.ShouldHave()
434438
.ViewBag(viewBag => viewBag
@@ -441,11 +445,11 @@ MyViewComponent<MyMvcController>
441445

442446
// View component unit test.
443447
MyViewComponent<MyMvcController>
444-
.Instance()
445-
.WithDependencies(
446-
serviceMock,
447-
anotherServiceMock,
448-
From.Services<IYetAnotherService>()) // Provides a global service.
448+
.Instance(instance => instance
449+
.WithDependencies(
450+
serviceMock,
451+
anotherServiceMock,
452+
From.Services<IYetAnotherService>())) // Provides a global service.
449453
.InvokedWith(c => c.InvokeAsync(1))
450454
.ShouldReturn()
451455
.View();
@@ -561,6 +565,49 @@ MyMvc
561565
.Ok();
562566
```
563567

568+
## Package Installation
569+
570+
You can install this library using [NuGet](https://www.nuget.org/packages/MyTested.AspNetCore.Mvc.Universe) into your test project (or reference it directly in your `.csproj` file). Currently **MyTested.AspNetCore.Mvc** is fully compatible with ASP.NET Core MVC 2.2.0 and all older versions available on the official NuGet feed.
571+
572+
```powershell
573+
Install-Package MyTested.AspNetCore.Mvc.Universe
574+
```
575+
576+
This package will include all available assertion methods in your test project, including ones for authentication, database, session, caching and more. If you want only the MVC related features, install `MyTested.AspNetCore.Mvc`. If you want to use the completely **FREE** and **UNLIMITED** version of the library, install only `MyTested.AspNetCore.Mvc.Lite` and no other package. Additionally, if you prefer, you can be more specific by including only some of the packages:
577+
578+
- `MyTested.AspNetCore.Mvc.Configuration` - Contains setup and assertion methods for configurations
579+
- `MyTested.AspNetCore.Mvc.Controllers` - Contains setup and assertion methods for controllers
580+
- `MyTested.AspNetCore.Mvc.Controllers.Attributes` - Contains setup and assertion methods for controller attributes
581+
- `MyTested.AspNetCore.Mvc.Controllers.ActionResults` - Contains setup and assertion methods for controller API action results
582+
- `MyTested.AspNetCore.Mvc.Controllers.Views` - Contains setup and assertion methods for controller view features
583+
- `MyTested.AspNetCore.Mvc.Controllers.Views.ActionResults` - Contains setup and assertion methods for controller view action results
584+
- `MyTested.AspNetCore.Mvc.Models` - Contains setup and assertion methods for response and view models
585+
- `MyTested.AspNetCore.Mvc.Routing` - Contains setup and assertion methods for routes
586+
- `MyTested.AspNetCore.Mvc.Core` - Contains setup and assertion methods for MVC core features
587+
- `MyTested.AspNetCore.Mvc.TempData` - Contains setup and assertion methods for `ITempDataDictionary`
588+
- `MyTested.AspNetCore.Mvc.ViewData` - Contains assertion methods for `ViewDataDictionary` and dynamic `ViewBag`
589+
- `MyTested.AspNetCore.Mvc.ViewComponents` - Contains setup and assertion methods for view components
590+
- `MyTested.AspNetCore.Mvc.ViewComponents.Attributes` - Contains setup and assertion methods for view component attributes
591+
- `MyTested.AspNetCore.Mvc.ViewComponents.Results` - Contains setup and assertion methods for view component results
592+
- `MyTested.AspNetCore.Mvc.ViewFeatures` - Contains setup and assertion methods for MVC view features
593+
- `MyTested.AspNetCore.Mvc.Http` - Contains setup and assertion methods for HTTP context, request and response
594+
- `MyTested.AspNetCore.Mvc.Authentication` - Contains setup methods for `ClaimsPrincipal`
595+
- `MyTested.AspNetCore.Mvc.ModelState` - Contains setup and assertion methods for `ModelStateDictionary` validations
596+
- `MyTested.AspNetCore.Mvc.DataAnnotations` - Contains setup and assertion methods for data annotation validations
597+
- `MyTested.AspNetCore.Mvc.EntityFrameworkCore` - Contains setup and assertion methods for `DbContext`
598+
- `MyTested.AspNetCore.Mvc.DependencyInjection` - Contains setup methods for dependency injection services
599+
- `MyTested.AspNetCore.Mvc.Caching` - Contains setup and assertion methods for `IMemoryCache`
600+
- `MyTested.AspNetCore.Mvc.Session` - Contains setup and assertion methods for `ISession`
601+
- `MyTested.AspNetCore.Mvc.Options` - Contains setup and assertion methods for `IOptions`
602+
- `MyTested.AspNetCore.Mvc.Helpers` - Contains additional helper methods for easier assertions
603+
- `MyTested.AspNetCore.Mvc.Lite` - Completely **FREE** and **UNLIMITED** version of the library. It should not be used in combination with any other package. Includes `Controllers`, `Controllers.Views` and `ViewComponents`.
604+
605+
After the downloading is complete, just add `using MyTested.AspNetCore.Mvc;` to your source code and you are ready to test in the most elegant and developer friendly way.
606+
607+
```c#
608+
using MyTested.AspNetCore.Mvc;
609+
```
610+
564611
## Versioning
565612

566613
**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.

samples/Blog/Blog.Test/Pipeline/ArticlesPipelineTest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ public void GetMineShouldReturnViewWithCorrectModel()
113113
.WithUser("Author Id 1", "Author 1"))
114114
.To<ArticlesController>(c => c.Mine())
115115
.Which(controller => controller
116-
.WithUser("Author Id 1", "Author 1")
117116
.WithData(ArticleTestData.GetArticles(2, sameUser: false)))
118117
.ShouldReturn()
119118
.View(view => view

0 commit comments

Comments
 (0)