Skip to content

Commit 9bd05d2

Browse files
committed
Added Options section to the tutorial (#132)
1 parent 0f43df6 commit 9bd05d2

File tree

9 files changed

+259
-11
lines changed

9 files changed

+259
-11
lines changed

docs/_docfx/tutorial/attributes.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public void CheckoutControllerShouldHaveAuthorizeAttribute()
1212
=> MyController<CheckoutController>
1313
.Instance()
1414
.ShouldHave()
15-
.Attributes(attributes => attributes
15+
.Attributes(attributes => attributes // <---
1616
.RestrictingForAuthorizedRequests());
1717
```
1818

@@ -60,7 +60,7 @@ public void RemoveAlbumConfirmedShouldHaveCorrectAttributes()
6060
With.No<int>(),
6161
With.No<CancellationToken>()))
6262
.ShouldHave()
63-
.ActionAttributes(attributes => attributes
63+
.ActionAttributes(attributes => attributes // <---
6464
.RestrictingForHttpMethod(HttpMethod.Post)
6565
.ChangingActionNameTo("RemoveAlbum"));
6666
```
@@ -85,7 +85,7 @@ public void LoginShouldHaveCorrectAttributes()
8585
.ActionAttributes(attributes => attributes
8686
.RestrictingForHttpMethod(HttpMethod.Post)
8787
.AllowingAnonymousRequests()
88-
.ContainingAttributeOfType<ValidateAntiForgeryTokenAttribute>());
88+
.ContainingAttributeOfType<ValidateAntiForgeryTokenAttribute>()); // <---
8989
```
9090

9191
The action is still invoked in this test so we need to provide a non-null value for the **"LoginViewModel"** parameter. A better approach on testing action attributes (without having to specify the parameters) will be available in the next major release of the library. :)
@@ -100,7 +100,7 @@ public void StoreManagerControllerShouldHaveCorrectAttributes()
100100
.ShouldHave()
101101
.Attributes(attributes => attributes
102102
.SpecifyingArea("Admin")
103-
.PassingFor<AuthorizeAttribute>(authorize => authorize.Policy == "ManageStore"));
103+
.PassingFor<AuthorizeAttribute>(authorize => authorize.Policy == "ManageStore")); // <---
104104
```
105105

106106
## Section summary

docs/_docfx/tutorial/options.md

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,74 @@
1-
# Options
1+
# Options
2+
3+
Remember this code?
4+
5+
```c#
6+
.WithServices(services => services
7+
.WithSetupFor<IOptions<AppSettings>>(settings => settings
8+
.Value.CacheDbResults = false))
9+
```
10+
11+
In this section we are going to improve it with the built-in options setup methods.
12+
13+
## Options configuration setup
14+
15+
Go to the **"project.json"** file and add **"MyTested.AspNetCore.Mvc.Options"** as a dependency of the test project:
16+
17+
```json
18+
"dependencies": {
19+
"dotnet-test-xunit": "2.2.0-*",
20+
"xunit": "2.2.0-*",
21+
"Moq": "4.6.38-*",
22+
"MyTested.AspNetCore.Mvc.Authentication": "1.0.0",
23+
"MyTested.AspNetCore.Mvc.Controllers": "1.0.0",
24+
"MyTested.AspNetCore.Mvc.DependencyInjection": "1.0.0",
25+
"MyTested.AspNetCore.Mvc.EntityFrameworkCore": "1.0.0",
26+
"MyTested.AspNetCore.Mvc.Http": "1.0.0",
27+
"MyTested.AspNetCore.Mvc.ModelState": "1.0.0",
28+
"MyTested.AspNetCore.Mvc.Models": "1.0.0",
29+
"MyTested.AspNetCore.Mvc.Options": "1.0.0", // <---
30+
"MyTested.AspNetCore.Mvc.ViewActionResults": "1.0.0",
31+
"MusicStore": "*"
32+
},
33+
```
34+
35+
Go to the unit test asserting the **"Details"** action in the **"StoreManagerControllerTest"** controller and change the following code:
36+
37+
```c#
38+
.WithServices(services => services
39+
.WithSetupFor<IOptions<AppSettings>>(settings => settings
40+
.Value.CacheDbResults = false))
41+
```
42+
43+
With this one:
44+
45+
```c#
46+
.WithOptions(options => options
47+
.For<AppSettings>(settings => settings.CacheDbResults = false))
48+
```
49+
50+
Much more readable! :)
51+
52+
Additionally, the **"TestStartup"** class no longer needs this call:
53+
54+
```c#
55+
services.AddScoped<IOptions<AppSettings>, OptionsManager<AppSettings>>();
56+
```
57+
58+
Our **"ConfigureTestServices"** should now contain the following:
59+
60+
```c#
61+
public void ConfigureTestServices(IServiceCollection services)
62+
{
63+
base.ConfigureServices(services);
64+
65+
services.ReplaceLifetime<IMemoryCache>(ServiceLifetime.Scoped);
66+
67+
services.ReplaceSingleton<SignInManager<ApplicationUser>>(sp =>
68+
MockProvider.SignInManager(sp.GetRequiredService<UserManager<ApplicationUser>>()));
69+
}
70+
```
71+
72+
## Section summary
73+
74+
Well, this was easy. In fact, it it is the easiest part of this tutorial. Let's move to [Session & Cache](/tutorial/sessioncache.html) where we will use the options setup one more time.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Session & Cache

docs/_docfx/tutorial/toc.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,10 @@
1818
- name: HTTP & Authentication
1919
href: httpauthentication.md
2020
- name: Licensing
21-
href: licensing.md
21+
href: licensing.md
22+
- name: Attributes
23+
href: attributes.md
24+
- name: Options
25+
href: options.md
26+
- name: Session & Cache
27+
href: sessioncache.md

docs/manifest.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

docs/tutorial/attributes.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ <h2 id="controller-attributes" sourcefile="tutorial/attributes.md" sourcestartli
7373
=&gt; MyController&lt;CheckoutController&gt;
7474
.Instance()
7575
.ShouldHave()
76-
.Attributes(attributes =&gt; attributes
76+
.Attributes(attributes =&gt; attributes // &lt;---
7777
.RestrictingForAuthorizedRequests());
7878
</code></pre><p sourcefile="tutorial/attributes.md" sourcestartlinenumber="19" sourceendlinenumber="19">Simple as that. Additionally, in the the <strong>&quot;HomeControllerTest&quot;</strong> class, we can add:</p>
7979
<pre sourcefile="tutorial/attributes.md" sourcestartlinenumber="21" sourceendlinenumber="28"><code class="lang-c#">[Fact]
@@ -104,7 +104,7 @@ <h2 id="controller-attributes" sourcefile="tutorial/attributes.md" sourcestartli
104104
With.No&lt;int&gt;(),
105105
With.No&lt;CancellationToken&gt;()))
106106
.ShouldHave()
107-
.ActionAttributes(attributes =&gt; attributes
107+
.ActionAttributes(attributes =&gt; attributes // &lt;---
108108
.RestrictingForHttpMethod(HttpMethod.Post)
109109
.ChangingActionNameTo(&quot;RemoveAlbum&quot;));
110110
</code></pre><p sourcefile="tutorial/attributes.md" sourcestartlinenumber="68" sourceendlinenumber="68">Working like a charm! :)</p>
@@ -122,7 +122,7 @@ <h2 id="custom-attributes" sourcefile="tutorial/attributes.md" sourcestartlinenu
122122
.ActionAttributes(attributes =&gt; attributes
123123
.RestrictingForHttpMethod(HttpMethod.Post)
124124
.AllowingAnonymousRequests()
125-
.ContainingAttributeOfType&lt;ValidateAntiForgeryTokenAttribute&gt;());
125+
.ContainingAttributeOfType&lt;ValidateAntiForgeryTokenAttribute&gt;()); // &lt;---
126126
</code></pre><p sourcefile="tutorial/attributes.md" sourcestartlinenumber="91" sourceendlinenumber="91">The action is still invoked in this test so we need to provide a non-null value for the <strong>&quot;LoginViewModel&quot;</strong> parameter. A better approach on testing action attributes (without having to specify the parameters) will be available in the next major release of the library. :)</p>
127127
<p sourcefile="tutorial/attributes.md" sourcestartlinenumber="93" sourceendlinenumber="93">Sometimes you may want to test specific property values of the attribute. You can use the <strong>&quot;PassingFor&quot;</strong> method:</p>
128128
<pre sourcefile="tutorial/attributes.md" sourcestartlinenumber="95" sourceendlinenumber="104"><code class="lang-c#">[Fact]
@@ -132,7 +132,7 @@ <h2 id="custom-attributes" sourcefile="tutorial/attributes.md" sourcestartlinenu
132132
.ShouldHave()
133133
.Attributes(attributes =&gt; attributes
134134
.SpecifyingArea(&quot;Admin&quot;)
135-
.PassingFor&lt;AuthorizeAttribute&gt;(authorize =&gt; authorize.Policy == &quot;ManageStore&quot;));
135+
.PassingFor&lt;AuthorizeAttribute&gt;(authorize =&gt; authorize.Policy == &quot;ManageStore&quot;)); // &lt;---
136136
</code></pre><h2 id="section-summary" sourcefile="tutorial/attributes.md" sourcestartlinenumber="106" sourceendlinenumber="106">Section summary</h2>
137137
<p sourcefile="tutorial/attributes.md" sourcestartlinenumber="108" sourceendlinenumber="108">We saw how easy it is to assert and validate all kinds of controller and action attributes. Now let&#39;s revisit our <a href="/tutorial/options.html" sourcefile="tutorial/attributes.md" sourcestartlinenumber="108" sourceendlinenumber="108">Options</a> testing!</p>
138138

docs/tutorial/options.html

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,50 @@
6565
<article class="content wrap" id="_content" data-uid="">
6666
<h1 id="options" sourcefile="tutorial/options.md" sourcestartlinenumber="1" sourceendlinenumber="1">Options</h1>
6767

68+
<p sourcefile="tutorial/options.md" sourcestartlinenumber="3" sourceendlinenumber="3">Remember this code?</p>
69+
<pre sourcefile="tutorial/options.md" sourcestartlinenumber="5" sourceendlinenumber="9"><code class="lang-c#">.WithServices(services =&gt; services
70+
.WithSetupFor&lt;IOptions&lt;AppSettings&gt;&gt;(settings =&gt; settings
71+
.Value.CacheDbResults = false))
72+
</code></pre><p sourcefile="tutorial/options.md" sourcestartlinenumber="11" sourceendlinenumber="11">In this section we are going to improve it with the built-in options setup methods.</p>
73+
<h2 id="options-configuration-setup" sourcefile="tutorial/options.md" sourcestartlinenumber="13" sourceendlinenumber="13">Options configuration setup</h2>
74+
<p sourcefile="tutorial/options.md" sourcestartlinenumber="15" sourceendlinenumber="15">Go to the <strong>&quot;project.json&quot;</strong> file and add <strong>&quot;MyTested.AspNetCore.Mvc.Options&quot;</strong> as a dependency of the test project:</p>
75+
<pre sourcefile="tutorial/options.md" sourcestartlinenumber="17" sourceendlinenumber="33"><code class="lang-json">&quot;dependencies&quot;: {
76+
&quot;dotnet-test-xunit&quot;: &quot;2.2.0-*&quot;,
77+
&quot;xunit&quot;: &quot;2.2.0-*&quot;,
78+
&quot;Moq&quot;: &quot;4.6.38-*&quot;,
79+
&quot;MyTested.AspNetCore.Mvc.Authentication&quot;: &quot;1.0.0&quot;,
80+
&quot;MyTested.AspNetCore.Mvc.Controllers&quot;: &quot;1.0.0&quot;,
81+
&quot;MyTested.AspNetCore.Mvc.DependencyInjection&quot;: &quot;1.0.0&quot;,
82+
&quot;MyTested.AspNetCore.Mvc.EntityFrameworkCore&quot;: &quot;1.0.0&quot;,
83+
&quot;MyTested.AspNetCore.Mvc.Http&quot;: &quot;1.0.0&quot;,
84+
&quot;MyTested.AspNetCore.Mvc.ModelState&quot;: &quot;1.0.0&quot;,
85+
&quot;MyTested.AspNetCore.Mvc.Models&quot;: &quot;1.0.0&quot;,
86+
&quot;MyTested.AspNetCore.Mvc.Options&quot;: &quot;1.0.0&quot;, // &lt;---
87+
&quot;MyTested.AspNetCore.Mvc.ViewActionResults&quot;: &quot;1.0.0&quot;,
88+
&quot;MusicStore&quot;: &quot;*&quot;
89+
},
90+
</code></pre><p sourcefile="tutorial/options.md" sourcestartlinenumber="35" sourceendlinenumber="35">Go to the unit test asserting the <strong>&quot;Details&quot;</strong> action in the <strong>&quot;StoreManagerControllerTest&quot;</strong> controller and change the following code:</p>
91+
<pre sourcefile="tutorial/options.md" sourcestartlinenumber="37" sourceendlinenumber="41"><code class="lang-c#">.WithServices(services =&gt; services
92+
.WithSetupFor&lt;IOptions&lt;AppSettings&gt;&gt;(settings =&gt; settings
93+
.Value.CacheDbResults = false))
94+
</code></pre><p sourcefile="tutorial/options.md" sourcestartlinenumber="43" sourceendlinenumber="43">With this one:</p>
95+
<pre sourcefile="tutorial/options.md" sourcestartlinenumber="45" sourceendlinenumber="48"><code class="lang-c#">.WithOptions(options =&gt; options
96+
.For&lt;AppSettings&gt;(settings =&gt; settings.CacheDbResults = false))
97+
</code></pre><p sourcefile="tutorial/options.md" sourcestartlinenumber="50" sourceendlinenumber="50">Much more readable! :)</p>
98+
<p sourcefile="tutorial/options.md" sourcestartlinenumber="52" sourceendlinenumber="52">Additionally, the <strong>&quot;TestStartup&quot;</strong> class no longer needs this call:</p>
99+
<pre sourcefile="tutorial/options.md" sourcestartlinenumber="54" sourceendlinenumber="56"><code class="lang-c#">services.AddScoped&lt;IOptions&lt;AppSettings&gt;, OptionsManager&lt;AppSettings&gt;&gt;();
100+
</code></pre><p sourcefile="tutorial/options.md" sourcestartlinenumber="58" sourceendlinenumber="58">Our <strong>&quot;ConfigureTestServices&quot;</strong> should now contain the following:</p>
101+
<pre sourcefile="tutorial/options.md" sourcestartlinenumber="60" sourceendlinenumber="70"><code class="lang-c#">public void ConfigureTestServices(IServiceCollection services)
102+
{
103+
base.ConfigureServices(services);
104+
105+
services.ReplaceLifetime&lt;IMemoryCache&gt;(ServiceLifetime.Scoped);
106+
107+
services.ReplaceSingleton&lt;SignInManager&lt;ApplicationUser&gt;&gt;(sp =&gt;
108+
MockProvider.SignInManager(sp.GetRequiredService&lt;UserManager&lt;ApplicationUser&gt;&gt;()));
109+
}
110+
</code></pre><h2 id="section-summary" sourcefile="tutorial/options.md" sourcestartlinenumber="72" sourceendlinenumber="72">Section summary</h2>
111+
<p sourcefile="tutorial/options.md" sourcestartlinenumber="74" sourceendlinenumber="74">Well, this was easy. In fact, it it is the easiest part of this tutorial. Let&#39;s move to <a href="/tutorial/sessioncache.html" sourcefile="tutorial/options.md" sourcestartlinenumber="74" sourceendlinenumber="74">Session &amp; Cache</a> where we will use the options setup one more time.</p>
68112

69113
</article>
70114
</div>

docs/tutorial/sessioncache.html

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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>Session &amp; Cache </title>
8+
<meta name="viewport" content="width=device-width">
9+
<meta name="title" content="Session &amp; Cache ">
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+
<h1 id="session--cache" sourcefile="tutorial/sessioncache.md" sourcestartlinenumber="1" sourceendlinenumber="1">Session &amp; Cache</h1>
67+
68+
69+
</article>
70+
</div>
71+
72+
<div class="hidden-sm col-md-2" role="complementary">
73+
<div class="sideaffix">
74+
<div class="contribution">
75+
<ul class="nav">
76+
<li>
77+
<a href="https://github.com/ivaylokenov/MyTested.AspNetCore.Mvc/blob/tutorial/docs/_docfx/tutorial/sessioncache.md/#L1" class="contribution-link">Improve this Doc</a>
78+
</li>
79+
</ul>
80+
</div>
81+
<nav class="bs-docs-sidebar hidden-print hidden-xs hidden-sm affix" id="affix">
82+
<!-- <p><a class="back-to-top" href="#top">Back to top</a><p> -->
83+
</nav>
84+
</div>
85+
</div>
86+
</div>
87+
</div>
88+
<footer>
89+
<div class="grad-bottom"></div>
90+
<div class="footer">
91+
<div class="container">
92+
<span class="pull-right">
93+
<a href="#top">Back to top</a>
94+
</span>
95+
<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>
96+
</div>
97+
</div>
98+
</footer>
99+
</div>
100+
101+
<script type="text/javascript" src="../styles/docfx.vendor.js"></script>
102+
<script>hljs.initHighlightingOnLoad();</script>
103+
<script type="text/javascript" src="../styles/docfx.js"></script>
104+
<script type="text/javascript" src="../styles/main.js"></script>
105+
</body>
106+
</html>

docs/tutorial/toc.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@
6767
<li>
6868
<a href="licensing.html" name="" title="Licensing">Licensing</a>
6969
</li>
70+
<li>
71+
<a href="attributes.html" name="" title="Attributes">Attributes</a>
72+
</li>
73+
<li>
74+
<a href="options.html" name="" title="Options">Options</a>
75+
</li>
76+
<li>
77+
<a href="sessioncache.html" name="" title="Session &amp; Cache">Session &amp; Cache</a>
78+
</li>
7079
</ul> </div>
7180
</div>
7281
</div>
@@ -107,6 +116,15 @@
107116
<li>
108117
<a href="licensing.html" name="" title="Licensing">Licensing</a>
109118
</li>
119+
<li>
120+
<a href="attributes.html" name="" title="Attributes">Attributes</a>
121+
</li>
122+
<li>
123+
<a href="options.html" name="" title="Options">Options</a>
124+
</li>
125+
<li>
126+
<a href="sessioncache.html" name="" title="Session &amp; Cache">Session &amp; Cache</a>
127+
</li>
110128
</ul> </div>
111129
</div>
112130
</div>

0 commit comments

Comments
 (0)