Skip to content

Commit dd11f11

Browse files
committed
Added Packages section to the tutorial (#132)
1 parent 75f4952 commit dd11f11

18 files changed

Lines changed: 555 additions & 8 deletions

docs/_docfx/guide/licensing.md

Whitespace-only changes.
74.1 KB
Loading
104 KB
Loading
40 Bytes
Binary file not shown.
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
# Controllers

docs/_docfx/tutorial/debugging.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Debugging Failed Tests
2+
3+
Now let's dive into [Controllers](/tutorial/controllers.html) testing!

docs/_docfx/tutorial/gettingstarted.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,24 @@ The JSON file is not optional and since we inherit from the original web **"Star
192192

193193
**Third**, as we noticed Visual Studio does not run the discovered tests for all specified frameworks. This is why we went to the console and tried running there. Unfortunately, the test failed for the **"net451"** framework. The reason is simple. The full framework did not save and store our project references and dependencies. This is why all the required test plugin classes could not be loaded when using "net451". By setting the **"preserveCompilationContext"** option to **"true"** the compiler will store the dependencies information into a file from where later during runtime it can be read successfully.
194194

195-
Well, all is well that ends well! While the **"Getting Started"** section of this tutorial may feel a bit "kaboom"-ish, it covers all the common failures and problems you may encounter while using My Tested ASP.NET Core MVC. From now on it is all unicorns and rainbows. Go to the [Testing Controllers](/tutorial/controllers.html) section and see for yourself! :)
195+
To finish this section let's make the test fail because of an invalid assertion just to see what happens. Instead of testing for **"View"**, make it assert for any other action result, for example **"BadRequest"**:
196+
197+
```c#
198+
[Fact]
199+
public void AddressAndPayment_ShouldReturn_DefaultView()
200+
=> MyController<CheckoutController>
201+
.Instance()
202+
.Calling(c => c.AddressAndPayment())
203+
.ShouldReturn()
204+
.BadRequest();
205+
```
206+
207+
Run the test and you will see a nice descriptive error message from My Tested ASP.NET Core MVC:
208+
209+
```
210+
When calling AddressAndPayment action in CheckoutController expected result to be BadRequestResult, but instead received ViewResult.
211+
```
212+
213+
Of course, you should undo the change and return the **"View"** call (unless you want a failing test during the whole tutorial but that's up to you again). :)
214+
215+
Well, all is well that ends well! While the **"Getting Started"** section of this tutorial may feel a bit "kaboom"-ish, it covers all the common failures and problems you may encounter while starting to use My Tested ASP.NET Core MVC. From now on it is all unicorns and rainbows. Go to the [Packages](/tutorial/packages.html) section and see for yourself! :)

docs/_docfx/tutorial/packages.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Packages
2+
3+
In this section we will learn the most important parts of arranging and asserting our web application components. Of course, as a main building block of the ASP.NET Core MVC framework, we will start with controllers.
4+
5+
Before we begin, let's make a step backwards. Remember the **"project.json"** and the referenced **"MyTested.AspNetCore.Mvc"** dependency? Good! The My Tested ASP.NET Core MVC framework consists of many packages. Here are the most important ones:
6+
7+
- **"MyTested.AspNetCore.Mvc.Core"** - Contains setup and assertion methods for MVC core features - controllers, models and routes
8+
- **"MyTested.AspNetCore.Mvc.DataAnnotations"** - Contains setup and assertion methods for data annotation validations and model state
9+
- **"MyTested.AspNetCore.Mvc.ViewFeatures"** - Contains setup and assertion methods for view action results, view data, view components and more
10+
- **"MyTested.AspNetCore.Mvc"** - Contains assertion methods for the full MVC framework
11+
12+
The packages above reflect the features in the ASP.NET Core packages - "**Microsoft.AspNetCore.Mvc.Core"**, "**Microsoft.AspNetCore.Mvc.DataAnnotations"**, "**Microsoft.AspNetCore.Mvc.ViewFeatures"** and "**Microsoft.AspNetCore.Mvc"**.
13+
14+
These so called "main" packages can be separated further to **"MyTested.AspNetCore.Mvc.Controllers"**, **"MyTested.AspNetCore.Mvc.Routing"**, **"MyTested.AspNetCore.Mvc.ViewActionResults"**, **"MyTested.AspNetCore.Mvc.ViewComponents"** and more. Helper packages for testing non-MVC related features like **"MyTested.AspNetCore.Mvc.EntityFrameworkCore"** and **"MyTested.AspNetCore.Mvc.Authentication"** are also available.
15+
16+
Additionally, these two packages are also available:
17+
18+
- **"MyTested.AspNetCore.Mvc.Universe"** - Combines all available binaries and the whole fluent API into one single package. Use it, if you do not want to include specific smaller packages and want to have every feature available.
19+
- **"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"**, `ViewActionResults"** and `ViewComponents"**.
20+
21+
Full list and descriptions of all available packages can be found [HERE](/guide/packages.html). All of them except the **"Lite"** one require a license code in order to be used without limitations. If a license code is not provided, a maximum of 100 assertions per test project is allowed. More information about the licensing can be found [HERE](/guide/licensing.html).
22+
23+
Now, let's get back to the testing. Go to the **"project.json"** file and replace the **"MyTested.AspNetCore.Mvc"** dependency with **"MyTested.AspNetCore.Mvc.Controllers"**. We will start using the small and specific packages for now and then we will switch to the **"Universe"** one later in the tutorial.
24+
25+
Your **"project.json"** dependencies should look like this:
26+
27+
```json
28+
"dependencies": {
29+
"dotnet-test-xunit": "2.2.0-*",
30+
"xunit": "2.2.0-*",
31+
"MyTested.AspNetCore.Mvc.Controllers": "1.0.0",
32+
"MusicStore": "*"
33+
},
34+
```
35+
36+
If you try to build the solution, you will receive an error stating that **"MyMvc"** does not exist.
37+
38+
<img src="/images/tutorial/mymvcdoesnotexist.jpg" alt="MyMvc no longer exists" />
39+
40+
The **"MyMvc"** class is only available in the **"MyTested.AspNetCore.Mvc"** package and combines all the different test types into a single starting point. To fix our test, we have to use the **"MyController"** class:
41+
42+
```c#
43+
[Fact]
44+
public void AddressAndPayment_ShouldReturn_DefaultView()
45+
=> MyController<CheckoutController>
46+
.Instance()
47+
.Calling(c => c.AddressAndPayment())
48+
.ShouldReturn()
49+
.View();
50+
```
51+
52+
Unfortunately, it still does not compile because the **"Controllers"** package contains assertions methods for the [ControllerBase](https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNetCore.Mvc.Core/ControllerBase.cs) class which does not include view features and action results.
53+
54+
You can see this by examining the intellisense of the test result:
55+
56+
<img src="/images/tutorial/coreintellisense.jpg" alt="Controllers package intellisense" />
57+
58+
We will now try the core action results before returning back to the view features. Comment the first test for now (so that the project will compile with the currently added dependencies) and add a new class named **"ManageControllerTest"** in the **"Controllers"** folder. We will test the asynchronous **"RemoveLogin"** action in the **"ManageController"**. If you examine it, you will notice that it returns **"RedirectToAction"**, if no user is authenticated.
59+
60+
Add the necessary usings and write the following test into the **"ManageControllerTest"** class:
61+
62+
```c#
63+
[Fact]
64+
public void RemoveLogin_ShouldReturn_RedirectToAction_WithNoUser()
65+
=> MyController<ManageController>
66+
.Instance()
67+
.Calling(c => c.RemoveLogin(null, null))
68+
.ShouldReturn()
69+
.Redirect();
70+
```
71+
72+
Run the test and it should pass correctly. As you can see My Tested ASP.NET Core MVC tests asynchronous actions flawlessly. If you do not like the null values, you may use the built-in helper class **"With"** to specify that there are no action arguments in this test:
73+
74+
```c#
75+
.Calling(c => c.RemoveLogin(With.No<string>(), With.No<string>()))
76+
```
77+
78+
As a bonus, let's assert some details of the redirect action result. We can see it redirects to the **"ManageLogins"** action with some **"ManageMessageId"** route value so we better test them:
79+
80+
```c#
81+
[Fact]
82+
public void RemoveLogin_ShouldReturn_RedirectToAction_WithNoUser()
83+
=> MyController<ManageController>
84+
.Instance()
85+
.Calling(c => c.RemoveLogin(
86+
With.No<string>(),
87+
With.No<string>()))
88+
.ShouldReturn()
89+
.Redirect()
90+
.ToAction(nameof(ManageController.ManageLogins))
91+
.ContainingRouteValues(new { Message = ManageController.ManageMessageId.Error });
92+
```
93+
94+
Now we can sleep peacefully! :)
95+
96+
OK, back to that commented test. We cannot test views with our current dependencies. Go to the **"project.json"** and add **"MyTested.AspNetCore.Mvc.ViewActionResults"** ас а dependency:
97+
98+
```json
99+
"dependencies": {
100+
"dotnet-test-xunit": "2.2.0-*",
101+
"xunit": "2.2.0-*",
102+
"MyTested.AspNetCore.Mvc.Controllers": "1.0.0",
103+
"MyTested.AspNetCore.Mvc.ViewActionResults": "1.0.0",
104+
"MusicStore": "*"
105+
},
106+
```
107+
108+
This package adds all action results from the [Controller](https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNetCore.Mvc.ViewFeatures/Controller.cs) class from the [ViewFeatures](https://github.com/aspnet/Mvc/tree/dev/src/Microsoft.AspNetCore.Mvc.ViewFeatures) MVC package. Go back to the **"CheckoutControllerTest"** class and uncomment the view test. It should compile and pass successfully now.
109+
110+
In this section we learned how we can use only these parts from My Tested ASP.NET Core MVC that we actually need in our testing project. As you can see each small package dependency adds additional extension methods to the fluent API. We will add more and more packages in the next sections so that you can get familiar with them. Next - [Debugging Failed Tests](/tutorial/debugging.html)!

docs/_docfx/tutorial/toc.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,9 @@
33
href: intro.md
44
- name: Getting Started
55
href: gettingstarted.md
6+
- name: Packages
7+
href: packages.md
8+
- name: Debugging Failed Tests
9+
href: debugging.md
610
- name: Controllers
711
href: controllers.md

docs/guide/licensing.html

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<!DOCTYPE html>
2+
<!--[if IE]><![endif]-->
3+
<html>
4+
<head>
5+
<meta charset="utf-8">
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
7+
<title> </title>
8+
<meta name="viewport" content="width=device-width">
9+
<meta name="title" content=" ">
10+
<meta name="generator" content="docfx 2.5.0.0">
11+
{% seo %}
12+
13+
<link rel="shortcut icon" href="../favicon.ico">
14+
<link rel="stylesheet" href="../styles/docfx.vendor.css">
15+
<link rel="stylesheet" href="../styles/docfx.css">
16+
<link rel="stylesheet" href="../styles/main.css">
17+
<meta property="docfx:navrel" content="../toc.html">
18+
<meta property="docfx:tocrel" content="toc.html">
19+
20+
</head>
21+
<body data-spy="scroll" data-target="#affix">
22+
<div id="wrapper">
23+
<header>
24+
<nav id="autocollapse" class="navbar navbar-inverse ng-scope" role="navigation">
25+
<div class="container">
26+
<div class="navbar-header">
27+
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar">
28+
<span class="sr-only">Toggle navigation</span>
29+
<span class="icon-bar"></span>
30+
<span class="icon-bar"></span>
31+
<span class="icon-bar"></span>
32+
</button>
33+
<a class="navbar-brand" href="../index.html">
34+
MY TESTED ASP.NET CORE MVC DOCS
35+
</a>
36+
</div>
37+
<div class="collapse navbar-collapse" id="navbar">
38+
<form class="navbar-form navbar-right" role="search" id="search">
39+
<div class="form-group">
40+
<input type="text" class="form-control" id="search-query" placeholder="Search" autocomplete="off">
41+
</div>
42+
</form>
43+
</div>
44+
</div>
45+
</nav>
46+
47+
<div class="subnav navbar navbar-default">
48+
<div class="container hide-when-search" id="breadcrumb">
49+
<ul class="breadcrumb">
50+
<li></li>
51+
</ul>
52+
</div>
53+
</div>
54+
</header>
55+
<div role="main" class="container body-content hide-when-search">
56+
57+
<div class="sidenav hide-when-search">
58+
<a class="btn toc-toggle collapse" data-toggle="collapse" href="#sidetoggle" aria-expanded="false" aria-controls="sidetoggle">Show / Hide Table of Contents</a>
59+
<div class="sidetoggle collapse" id="sidetoggle">
60+
<div id="sidetoc"></div>
61+
</div>
62+
</div>
63+
<div class="article row grid-right">
64+
<div class="col-md-10">
65+
<article class="content wrap" id="_content" data-uid="">
66+
67+
68+
</article>
69+
</div>
70+
71+
<div class="hidden-sm col-md-2" role="complementary">
72+
<div class="sideaffix">
73+
<div class="contribution">
74+
<ul class="nav">
75+
<li>
76+
<a href="https://github.com/ivaylokenov/MyTested.AspNetCore.Mvc/blob/tutorial/docs/_docfx/guide/licensing.md/#L1" class="contribution-link">Improve this Doc</a>
77+
</li>
78+
</ul>
79+
</div>
80+
<nav class="bs-docs-sidebar hidden-print hidden-xs hidden-sm affix" id="affix">
81+
<!-- <p><a class="back-to-top" href="#top">Back to top</a><p> -->
82+
</nav>
83+
</div>
84+
</div>
85+
</div>
86+
</div>
87+
<footer>
88+
<div class="grad-bottom"></div>
89+
<div class="footer">
90+
<div class="container">
91+
<span class="pull-right">
92+
<a href="#top">Back to top</a>
93+
</span>
94+
<span>Copyright © 2015-2016 <strong><a href="http://mytestedasp.net">MyTestedASP.NET</a></strong>. All Rights Reserved. Generated by <strong><a href="http://dotnet.github.io/docfx/">DocFX</a></strong></span>
95+
</div>
96+
</div>
97+
</footer>
98+
</div>
99+
100+
<script type="text/javascript" src="../styles/docfx.vendor.js"></script>
101+
<script>hljs.initHighlightingOnLoad();</script>
102+
<script type="text/javascript" src="../styles/docfx.js"></script>
103+
<script type="text/javascript" src="../styles/main.js"></script>
104+
</body>
105+
</html>

0 commit comments

Comments
 (0)