Skip to content

Commit f04e6db

Browse files
committed
Updated tutorial HTML files
1 parent ace57b2 commit f04e6db

22 files changed

+1953
-1741
lines changed

docs/tutorial/attributes.html

Lines changed: 68 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!DOCTYPE html>
1+
<!DOCTYPE html>
22
<!--[if IE]><![endif]-->
33
<html>
44
<head>
@@ -7,13 +7,13 @@
77
<title>Attributes </title>
88
<meta name="viewport" content="width=device-width">
99
<meta name="title" content="Attributes ">
10-
<meta name="generator" content="docfx 2.6.3.0">
10+
<meta name="generator" content="docfx 2.45.1.0">
1111

1212
<link rel="shortcut icon" href="../favicon.ico">
1313
<link rel="stylesheet" href="../styles/docfx.vendor.css">
1414
<link rel="stylesheet" href="../styles/docfx.css">
1515
<link rel="stylesheet" href="../styles/main.css">
16-
<meta property="docfx:navrel" content="../toc.html">
16+
<meta property="docfx:navrel" content="">
1717
<meta property="docfx:tocrel" content="toc.html">
1818
<meta property="docfx:rel" content="../">
1919
<meta property="og:title" content="Flexible and asynchronous testing framework for ASP.NET Core MVC">
@@ -30,7 +30,7 @@
3030
<meta property="twitter:url" content="https://mytestedasp.net/">
3131
<meta property="twitter:image" content="https://mytestedasp.net/Content/Images/logosocial.jpg">
3232
</head>
33-
<body data-spy="scroll" data-target="#affix">
33+
<body data-spy="scroll" data-target="#affix" data-offset="120">
3434
<div id="wrapper">
3535
<header>
3636
<nav id="autocollapse" class="navbar navbar-inverse ng-scope" role="navigation">
@@ -75,55 +75,81 @@
7575
<div class="article row grid-right">
7676
<div class="col-md-10">
7777
<article class="content wrap" id="_content" data-uid="">
78-
<h1 id="attributes" sourcefile="tutorial/attributes.md" sourcestartlinenumber="1" sourceendlinenumber="1">Attributes</h1>
79-
80-
<p sourcefile="tutorial/attributes.md" sourcestartlinenumber="3" sourceendlinenumber="3">This section will cover testing of attributes and their properties.</p>
81-
<h2 id="controller-attributes" sourcefile="tutorial/attributes.md" sourcestartlinenumber="5" sourceendlinenumber="5">Controller attributes</h2>
82-
<p sourcefile="tutorial/attributes.md" sourcestartlinenumber="7" sourceendlinenumber="7">Let&#39;s assert that our <strong>&quot;CheckoutController&quot;</strong> is decorated with the commonly used <strong>&quot;AuthorizeAttribute&quot;</strong>. Go to the <strong>CheckoutControllerTest&quot;</strong> class and add the following test:</p>
83-
<pre sourcefile="tutorial/attributes.md" sourcestartlinenumber="9" sourceendlinenumber="17"><code class="lang-c#">[Fact]
78+
<h1 id="attributes">Attributes</h1>
79+
80+
<p>This section will cover testing of attributes and their properties.</p>
81+
<h2 id="controller-attributes">Controller attributes</h2>
82+
<p>First, we need another package. Again?! </p>
83+
<p>Do not worry, later in this tutorial we are going to use the <strong>&quot;Universe&quot;</strong> package and this madness will end. The purpose here is to show you the different components of the testing framework.</p>
84+
<p>Now, obey the rules and add <strong>&quot;MyTested.AspNetCore.Mvc.Controllers.Attributes&quot;</strong> to your <strong>&quot;MusicStore.Test.csproj&quot;</strong> project:</p>
85+
<pre><code class="lang-xml">&lt;!-- Other ItemGroups --&gt;
86+
87+
&lt;ItemGroup&gt;
88+
&lt;PackageReference Include=&quot;Microsoft.AspNetCore.App&quot; /&gt;
89+
&lt;PackageReference Include=&quot;Microsoft.NET.Test.Sdk&quot; Version=&quot;16.0.1&quot; /&gt;
90+
&lt;PackageReference Include=&quot;Moq&quot; Version=&quot;4.13.0&quot; /&gt;
91+
&lt;PackageReference Include=&quot;MyTested.AspNetCore.Mvc.Authentication&quot; Version=&quot;2.2.0&quot; /&gt;
92+
&lt;PackageReference Include=&quot;MyTested.AspNetCore.Mvc.Controllers&quot; Version=&quot;2.2.0&quot; /&gt;
93+
&lt;PackageReference Include=&quot;MyTested.AspNetCore.Mvc.Controllers.ActionResults&quot; Version=&quot;2.2.0&quot; /&gt;
94+
&lt;!-- MyTested.AspNetCore.Mvc.Controllers.Attributes package --&gt;
95+
&lt;PackageReference Include=&quot;MyTested.AspNetCore.Mvc.Controllers.Attributes&quot; Version=&quot;2.2.0&quot; /&gt;
96+
&lt;PackageReference Include=&quot;MyTested.AspNetCore.Mvc.Controllers.Views&quot; Version=&quot;2.2.0&quot; /&gt;
97+
&lt;PackageReference Include=&quot;MyTested.AspNetCore.Mvc.Controllers.Views.ActionResults&quot; Version=&quot;2.2.0&quot; /&gt;
98+
&lt;PackageReference Include=&quot;MyTested.AspNetCore.Mvc.DependencyInjection&quot; Version=&quot;2.2.0&quot; /&gt;
99+
&lt;PackageReference Include=&quot;MyTested.AspNetCore.Mvc.EntityFrameworkCore&quot; Version=&quot;2.2.0&quot; /&gt;
100+
&lt;PackageReference Include=&quot;MyTested.AspNetCore.Mvc.Http&quot; Version=&quot;2.2.0&quot; /&gt;
101+
&lt;PackageReference Include=&quot;MyTested.AspNetCore.Mvc.Models&quot; Version=&quot;2.2.0&quot; /&gt;
102+
&lt;PackageReference Include=&quot;MyTested.AspNetCore.Mvc.ModelState&quot; Version=&quot;2.2.0&quot; /&gt;
103+
&lt;PackageReference Include=&quot;xunit&quot; Version=&quot;2.4.0&quot; /&gt;
104+
&lt;PackageReference Include=&quot;xunit.runner.visualstudio&quot; Version=&quot;2.4.0&quot; /&gt;
105+
&lt;/ItemGroup&gt;
106+
107+
&lt;!-- Other ItemGroups --&gt;
108+
</code></pre><p>Let&#39;s assert that our <strong>&quot;CheckoutController&quot;</strong> is decorated with the commonly used <strong>&quot;AuthorizeAttribute&quot;</strong>. Go to the <strong>CheckoutControllerTest&quot;</strong> class and add the following test:</p>
109+
<pre><code class="lang-c#">[Fact]
84110
public void CheckoutControllerShouldHaveAuthorizeAttribute()
85111
=&gt; MyController&lt;CheckoutController&gt;
86112
.Instance()
87113
.ShouldHave()
88114
.Attributes(attributes =&gt; attributes // &lt;---
89115
.RestrictingForAuthorizedRequests());
90-
</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>
91-
<pre sourcefile="tutorial/attributes.md" sourcestartlinenumber="21" sourceendlinenumber="28"><code class="lang-c#">[Fact]
116+
</code></pre><p>Simple as that. Additionally, in the the <strong>&quot;HomeControllerTest&quot;</strong> class, we can add:</p>
117+
<pre><code class="lang-c#">[Fact]
92118
public void HomeControllerShouldHaveNoAttributes()
93119
=&gt; MyController&lt;HomeController&gt;
94120
.Instance()
95121
.ShouldHave()
96122
.NoAttributes();
97-
</code></pre><p sourcefile="tutorial/attributes.md" sourcestartlinenumber="30" sourceendlinenumber="30">Of course, if you change <strong>&quot;NoAttributes&quot;</strong> to <strong>&quot;Attributes&quot;</strong>, you will receive an error:</p>
98-
<pre sourcefile="tutorial/attributes.md" sourcestartlinenumber="32" sourceendlinenumber="34"><code class="lang-text">When testing HomeController was expected to have at least 1 attribute, but in fact none was found.
99-
</code></pre><h2 id="action-attributes" sourcefile="tutorial/attributes.md" sourcestartlinenumber="36" sourceendlinenumber="36">Action attributes</h2>
100-
<p sourcefile="tutorial/attributes.md" sourcestartlinenumber="38" sourceendlinenumber="38">Action attributes are not different. Let&#39;s test the <strong>&quot;RemoveAlbumConfirmed&quot;</strong> action in the <strong>&quot;StoreManagerController&quot;</strong>:</p>
101-
<pre sourcefile="tutorial/attributes.md" sourcestartlinenumber="40" sourceendlinenumber="49"><code class="lang-c#">[HttpPost, ActionName(&quot;RemoveAlbum&quot;)]
123+
</code></pre><p>Of course, if you change <strong>&quot;NoAttributes&quot;</strong> to <strong>&quot;Attributes&quot;</strong>, you will receive an error:</p>
124+
<pre><code class="lang-text">When testing HomeController was expected to have at least 1 attribute, but in fact none was found.
125+
</code></pre><h2 id="action-attributes">Action attributes</h2>
126+
<p>Action attributes are not different. Let&#39;s test the <strong>&quot;RemoveAlbumConfirmed&quot;</strong> action in the <strong>&quot;StoreManagerController&quot;</strong>:</p>
127+
<pre><code class="lang-c#">[HttpPost, ActionName(&quot;RemoveAlbum&quot;)]
102128
public async Task&lt;IActionResult&gt; RemoveAlbumConfirmed(
103129
[FromServices] IMemoryCache cache,
104130
int id,
105131
CancellationToken requestAborted)
106132
{
107133

108134
// action code skipped for brevity
109-
</code></pre><p sourcefile="tutorial/attributes.md" sourcestartlinenumber="51" sourceendlinenumber="51">We need to test the <strong>&quot;HttpPost&quot;</strong> and <strong>&quot;ActionName&quot;</strong> attributes:</p>
110-
<pre sourcefile="tutorial/attributes.md" sourcestartlinenumber="53" sourceendlinenumber="66"><code class="lang-c#">[Fact]
135+
</code></pre><p>We need to test the <strong>&quot;HttpPost&quot;</strong> and <strong>&quot;ActionName&quot;</strong> attributes:</p>
136+
<pre><code class="lang-c#">[Fact]
111137
public void RemoveAlbumConfirmedShouldHaveCorrectAttributes()
112138
=&gt; MyController&lt;StoreManagerController&gt;
113-
.Instance()
114-
.Calling(c =&gt; c.RemoveAlbumConfirmed(
115-
With.No&lt;IMemoryCache&gt;(),
116-
With.No&lt;int&gt;(),
117-
With.No&lt;CancellationToken&gt;()))
118-
.ShouldHave()
119-
.ActionAttributes(attributes =&gt; attributes // &lt;---
120-
.RestrictingForHttpMethod(HttpMethod.Post)
121-
.ChangingActionNameTo(&quot;RemoveAlbum&quot;));
122-
</code></pre><p sourcefile="tutorial/attributes.md" sourcestartlinenumber="68" sourceendlinenumber="68">Working like a charm! :)</p>
123-
<h2 id="custom-attributes" sourcefile="tutorial/attributes.md" sourcestartlinenumber="70" sourceendlinenumber="70">Custom attributes</h2>
124-
<p sourcefile="tutorial/attributes.md" sourcestartlinenumber="72" sourceendlinenumber="72">Sometimes you will have custom attributes which are not available in the fluent testing API. For example, you may have noticed that there is no method to test the <strong>&quot;ValidateAntiForgeryTokenAttribute&quot;</strong>&quot;. Actually, it&#39;s in the <strong>&quot;ViewFeatures&quot;</strong> package, but you don&#39;t know that! :)</p>
125-
<p sourcefile="tutorial/attributes.md" sourcestartlinenumber="74" sourceendlinenumber="74">Let&#39;s see an example and test the HTTP Post <strong>&quot;Login&quot;</strong> action in the <strong>&quot;AccountController&quot;</strong>. It has these three attributes - <strong>&quot;HttpPost&quot;</strong>, <strong>&quot;AllowAnonymous&quot;</strong>, <strong>&quot;ValidateAntiForgeryToken&quot;</strong>. For the latter you can use the <strong>&quot;ContainingAttributeOfType&quot;</strong> method:</p>
126-
<pre sourcefile="tutorial/attributes.md" sourcestartlinenumber="76" sourceendlinenumber="89"><code class="lang-c#">[Fact]
139+
.Instance()
140+
.Calling(c =&gt; c.RemoveAlbumConfirmed(
141+
With.No&lt;IMemoryCache&gt;(),
142+
With.No&lt;int&gt;(),
143+
With.No&lt;CancellationToken&gt;()))
144+
.ShouldHave()
145+
.ActionAttributes(attributes =&gt; attributes // &lt;---
146+
.RestrictingForHttpMethod(HttpMethod.Post)
147+
.ChangingActionNameTo(&quot;RemoveAlbum&quot;));
148+
</code></pre><p>Working like a charm! :)</p>
149+
<h2 id="custom-attributes">Custom attributes</h2>
150+
<p>Sometimes you will have custom attributes which are not available in the fluent testing API. For example, you may have noticed that there is no method to test the <strong>&quot;ValidateAntiForgeryTokenAttribute&quot;</strong>&quot;. Actually, it&#39;s in the <strong>&quot;ViewFeatures&quot;</strong> package, but you don&#39;t know that! :)</p>
151+
<p>Let&#39;s see an example and test the HTTP Post <strong>&quot;Login&quot;</strong> action in the <strong>&quot;AccountController&quot;</strong>. It has these three attributes - <strong>&quot;HttpPost&quot;</strong>, <strong>&quot;AllowAnonymous&quot;</strong>, <strong>&quot;ValidateAntiForgeryToken&quot;</strong>. For the latter you can use the <strong>&quot;ContainingAttributeOfType&quot;</strong> method:</p>
152+
<pre><code class="lang-c#">[Fact]
127153
public void LoginShouldHaveCorrectAttributes()
128154
=&gt; MyController&lt;AccountController&gt;
129155
.Instance()
@@ -135,28 +161,28 @@ <h2 id="custom-attributes" sourcefile="tutorial/attributes.md" sourcestartlinenu
135161
.RestrictingForHttpMethod(HttpMethod.Post)
136162
.AllowingAnonymousRequests()
137163
.ContainingAttributeOfType&lt;ValidateAntiForgeryTokenAttribute&gt;()); // &lt;---
138-
</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 for testing action attributes (without having to specify the parameters) will be available in the next major release of the library. :)</p>
139-
<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>
140-
<pre sourcefile="tutorial/attributes.md" sourcestartlinenumber="95" sourceendlinenumber="104"><code class="lang-c#">[Fact]
164+
</code></pre><p>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 for testing action attributes (without having to specify the parameters) will be available in the next major release of the library. :)</p>
165+
<p>Sometimes you may want to test specific property values of the attribute. You can use the <strong>&quot;PassingFor&quot;</strong> method:</p>
166+
<pre><code class="lang-c#">[Fact]
141167
public void StoreManagerControllerShouldHaveCorrectAttributes()
142168
=&gt; MyController&lt;StoreManagerController&gt;
143169
.Instance()
144170
.ShouldHave()
145171
.Attributes(attributes =&gt; attributes
146172
.SpecifyingArea(&quot;Admin&quot;)
147-
.PassingFor&lt;AuthorizeAttribute&gt;(authorize =&gt; authorize.Policy == &quot;ManageStore&quot;));
148-
</code></pre><h2 id="section-summary" sourcefile="tutorial/attributes.md" sourcestartlinenumber="106" sourceendlinenumber="106">Section summary</h2>
149-
<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. But enough about them - in the next section we will cover thrown <a href="/tutorial/exceptions.html" sourcefile="tutorial/attributes.md" sourcestartlinenumber="108" sourceendlinenumber="108">Exceptions</a>.</p>
150-
151-
</article>
173+
.PassingFor&lt;AuthorizeAttribute&gt;(authorize =&gt; authorize // &lt;---
174+
.Policy == &quot;ManageStore&quot;));
175+
</code></pre><h2 id="section-summary">Section summary</h2>
176+
<p>We saw how easy it is to assert and validate all kinds of controller and action attributes. But enough about them - in the next section we will cover thrown <a href="/tutorial/exceptions.html">Exceptions</a>.</p>
177+
</article>
152178
</div>
153179

154180
<div class="hidden-sm col-md-2" role="complementary">
155181
<div class="sideaffix">
156182
<div class="contribution">
157183
<ul class="nav">
158184
<li>
159-
<a href="https://github.com/ivaylokenov/MyTested.AspNetCore.Mvc/blob/tutorial/docs/_docfx/tutorial/attributes.md/#L1" class="contribution-link">Improve this Doc</a>
185+
<a href="https://github.com/tmollov/MyTested.AspNetCore.Mvc/blob/development/docs/_docfx/tutorial/attributes.md/#L1" class="contribution-link">Improve this Doc</a>
160186
</li>
161187
</ul>
162188
</div>

0 commit comments

Comments
 (0)