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
<psourcefile="tutorial/attributes.md" sourcestartlinenumber="3" sourceendlinenumber="3">This section will cover testing of attributes and their properties.</p>
<psourcefile="tutorial/attributes.md" sourcestartlinenumber="7" sourceendlinenumber="7">Let's assert that our <strong>"CheckoutController"</strong> is decorated with the commonly used <strong>"AuthorizeAttribute"</strong>. Go to the <strong>CheckoutControllerTest"</strong> class and add the following test:</p>
<p>Do not worry, later in this tutorial we are going to use the <strong>"Universe"</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>"MyTested.AspNetCore.Mvc.Controllers.Attributes"</strong> to your <strong>"MusicStore.Test.csproj"</strong> project:</p>
85
+
<pre><codeclass="lang-xml"><!-- Other ItemGroups -->
</code></pre><p>Let's assert that our <strong>"CheckoutController"</strong> is decorated with the commonly used <strong>"AuthorizeAttribute"</strong>. Go to the <strong>CheckoutControllerTest"</strong> class and add the following test:</p>
109
+
<pre><codeclass="lang-c#">[Fact]
84
110
public void CheckoutControllerShouldHaveAuthorizeAttribute()
</code></pre><psourcefile="tutorial/attributes.md" sourcestartlinenumber="19" sourceendlinenumber="19">Simple as that. Additionally, in the the <strong>"HomeControllerTest"</strong> class, we can add:</p>
</code></pre><p>Simple as that. Additionally, in the the <strong>"HomeControllerTest"</strong> class, we can add:</p>
117
+
<pre><codeclass="lang-c#">[Fact]
92
118
public void HomeControllerShouldHaveNoAttributes()
93
119
=> MyController<HomeController>
94
120
.Instance()
95
121
.ShouldHave()
96
122
.NoAttributes();
97
-
</code></pre><psourcefile="tutorial/attributes.md" sourcestartlinenumber="30" sourceendlinenumber="30">Of course, if you change <strong>"NoAttributes"</strong> to <strong>"Attributes"</strong>, you will receive an error:</p>
98
-
<presourcefile="tutorial/attributes.md" sourcestartlinenumber="32" sourceendlinenumber="34"><codeclass="lang-text">When testing HomeController was expected to have at least 1 attribute, but in fact none was found.
<psourcefile="tutorial/attributes.md" sourcestartlinenumber="38" sourceendlinenumber="38">Action attributes are not different. Let's test the <strong>"RemoveAlbumConfirmed"</strong> action in the <strong>"StoreManagerController"</strong>:</p>
</code></pre><p>Of course, if you change <strong>"NoAttributes"</strong> to <strong>"Attributes"</strong>, you will receive an error:</p>
124
+
<pre><codeclass="lang-text">When testing HomeController was expected to have at least 1 attribute, but in fact none was found.
<p>Action attributes are not different. Let's test the <strong>"RemoveAlbumConfirmed"</strong> action in the <strong>"StoreManagerController"</strong>:</p>
public async Task<IActionResult> RemoveAlbumConfirmed(
103
129
[FromServices] IMemoryCache cache,
104
130
int id,
105
131
CancellationToken requestAborted)
106
132
{
107
133
108
134
// action code skipped for brevity
109
-
</code></pre><psourcefile="tutorial/attributes.md" sourcestartlinenumber="51" sourceendlinenumber="51">We need to test the <strong>"HttpPost"</strong> and <strong>"ActionName"</strong> attributes:</p>
<psourcefile="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>"ValidateAntiForgeryTokenAttribute"</strong>". Actually, it's in the <strong>"ViewFeatures"</strong> package, but you don't know that! :)</p>
125
-
<psourcefile="tutorial/attributes.md" sourcestartlinenumber="74" sourceendlinenumber="74">Let's see an example and test the HTTP Post <strong>"Login"</strong> action in the <strong>"AccountController"</strong>. It has these three attributes - <strong>"HttpPost"</strong>, <strong>"AllowAnonymous"</strong>, <strong>"ValidateAntiForgeryToken"</strong>. For the latter you can use the <strong>"ContainingAttributeOfType"</strong> method:</p>
<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>"ValidateAntiForgeryTokenAttribute"</strong>". Actually, it's in the <strong>"ViewFeatures"</strong> package, but you don't know that! :)</p>
151
+
<p>Let's see an example and test the HTTP Post <strong>"Login"</strong> action in the <strong>"AccountController"</strong>. It has these three attributes - <strong>"HttpPost"</strong>, <strong>"AllowAnonymous"</strong>, <strong>"ValidateAntiForgeryToken"</strong>. For the latter you can use the <strong>"ContainingAttributeOfType"</strong> method:</p>
</code></pre><psourcefile="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>"LoginViewModel"</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
-
<psourcefile="tutorial/attributes.md" sourcestartlinenumber="93" sourceendlinenumber="93">Sometimes you may want to test specific property values of the attribute. You can use the <strong>"PassingFor"</strong> method:</p>
</code></pre><p>The action is still invoked in this test, so we need to provide a non-null value for the <strong>"LoginViewModel"</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>"PassingFor"</strong> method:</p>
166
+
<pre><codeclass="lang-c#">[Fact]
141
167
public void StoreManagerControllerShouldHaveCorrectAttributes()
<psourcefile="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 <ahref="/tutorial/exceptions.html" sourcefile="tutorial/attributes.md" sourcestartlinenumber="108" sourceendlinenumber="108">Exceptions</a>.</p>
<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 <ahref="/tutorial/exceptions.html">Exceptions</a>.</p>
<ahref="https://github.com/ivaylokenov/MyTested.AspNetCore.Mvc/blob/tutorial/docs/_docfx/tutorial/attributes.md/#L1" class="contribution-link">Improve this Doc</a>
185
+
<ahref="https://github.com/tmollov/MyTested.AspNetCore.Mvc/blob/development/docs/_docfx/tutorial/attributes.md/#L1" class="contribution-link">Improve this Doc</a>
0 commit comments