Skip to content

Commit 0f43df6

Browse files
committed
Added Attributes section to the tutorial (#132)
1 parent 6fe0307 commit 0f43df6

9 files changed

Lines changed: 665 additions & 1 deletion

File tree

21.4 KB
Loading

docs/_docfx/tutorial/attributes.md

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# Attributes
2+
3+
This section will cover testing of attributes and their properties.
4+
5+
## Controller attributes
6+
7+
Let's assert that our **"CheckoutController"** is decorated with the commonly used **"AuthorizeAttribute"**. Go to the **CheckoutControllerTest"** class and add the following test:
8+
9+
```c#
10+
[Fact]
11+
public void CheckoutControllerShouldHaveAuthorizeAttribute()
12+
=> MyController<CheckoutController>
13+
.Instance()
14+
.ShouldHave()
15+
.Attributes(attributes => attributes
16+
.RestrictingForAuthorizedRequests());
17+
```
18+
19+
Simple as that. Additionally, in the the **"HomeControllerTest"** class, we can add:
20+
21+
```c#
22+
[Fact]
23+
public void HomeControllerShouldHaveNoAttributes()
24+
=> MyController<HomeController>
25+
.Instance()
26+
.ShouldHave()
27+
.NoAttributes();
28+
```
29+
30+
Of course, if you change **"NoAttributes"** to **"Attributes"**, you will receive an error:
31+
32+
```
33+
When testing HomeController was expected to have at least 1 attribute, but in fact none was found.
34+
```
35+
36+
## Action attributes
37+
38+
Action attributes are not different. Let's test the **"RemoveAlbumConfirmed"** action in the **"StoreManagerController"**:
39+
40+
```c#
41+
[HttpPost, ActionName("RemoveAlbum")]
42+
public async Task<IActionResult> RemoveAlbumConfirmed(
43+
[FromServices] IMemoryCache cache,
44+
int id,
45+
CancellationToken requestAborted)
46+
{
47+
48+
// action code skipped for brevity
49+
```
50+
51+
We need to test the **"HttpPost"** and **"ActionName"** attributes:
52+
53+
```c#
54+
[Fact]
55+
public void RemoveAlbumConfirmedShouldHaveCorrectAttributes()
56+
=> MyController<StoreManagerController>
57+
.Instance()
58+
.Calling(c => c.RemoveAlbumConfirmed(
59+
With.No<IMemoryCache>(),
60+
With.No<int>(),
61+
With.No<CancellationToken>()))
62+
.ShouldHave()
63+
.ActionAttributes(attributes => attributes
64+
.RestrictingForHttpMethod(HttpMethod.Post)
65+
.ChangingActionNameTo("RemoveAlbum"));
66+
```
67+
68+
Working like a charm! :)
69+
70+
## Custom attributes
71+
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 **"ValidateAntiForgeryTokenAttribute"**". Actually it is in the **"ViewFeatures"** package but you don't know that! :)
73+
74+
Let's see an example and test the HTTP Post **"Login"** action in the **"AccountController"**. It has these three attributes - **"HttpPost"**, **"AllowAnonymous"**, **"ValidateAntiForgeryToken"**. For the latter you can use the **"ContainingAttributeOfType"** method:
75+
76+
```c#
77+
[Fact]
78+
public void LoginShouldHaveCorrectAttributes()
79+
=> MyController<AccountController>
80+
.Instance()
81+
.Calling(c => c.Login(
82+
With.Default<LoginViewModel>(),
83+
With.No<string>()))
84+
.ShouldHave()
85+
.ActionAttributes(attributes => attributes
86+
.RestrictingForHttpMethod(HttpMethod.Post)
87+
.AllowingAnonymousRequests()
88+
.ContainingAttributeOfType<ValidateAntiForgeryTokenAttribute>());
89+
```
90+
91+
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. :)
92+
93+
Sometimes you may want to test specific property values of the attribute. You can use the **"PassingFor"** method:
94+
95+
```c#
96+
[Fact]
97+
public void StoreManagerControllerShouldHaveCorrectAttributes()
98+
=> MyController<StoreManagerController>
99+
.Instance()
100+
.ShouldHave()
101+
.Attributes(attributes => attributes
102+
.SpecifyingArea("Admin")
103+
.PassingFor<AuthorizeAttribute>(authorize => authorize.Policy == "ManageStore"));
104+
```
105+
106+
## Section summary
107+
108+
We saw how easy it is to assert and validate all kinds of controller and action attributes. Now let's revisit our [Options](/tutorial/options.html) testing!

docs/_docfx/tutorial/licensing.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Licensing
2+
3+
This section will describe all available My Tested ASP.NET Core MVC editions and their licensing options.
4+
5+
## Editions
6+
7+
The testing library has four different editions available for you to explore:
8+
9+
### Lite
10+
11+
The **"Lite"** edition of the library provides only a small subset of the available setups and assertions but they are more than sufficient for controller and view component testing.
12+
13+
- Perfect for small MVC applications
14+
- Completely free and unlimited
15+
- Includes only **"Controllers"**, **"ViewActionResults"** and **"ViewComponents"**
16+
- Cannot be used in combination with any other package
17+
- Requires manual mock creation of commonly used services
18+
- Can be used by everyone
19+
- [Apache or Microsoft Public License](https://github.com/ivaylokenov/MyTested.AspNetCore.Mvc/blob/development/src/MyTested.AspNetCore.Mvc.Lite/LICENSE)
20+
21+
### Community
22+
23+
The **"Community*"" edition of the library is fully unlocked and the whole fluent testing API can be used for free. This edition is exclusively available only for individuals, open-source projects, startups and educational institutions.
24+
25+
- Perfect for every MVC application complying with the special terms
26+
- Completely free and unlimited
27+
- Developers can include [any package they prefer](/guide/packages.html)
28+
- Available only for individuals and special types of businesses and organizations
29+
- Fully featured edition
30+
- License code can be requested from [HERE](https://mytestedasp.net/Core/Mvc#free-usage-modal) for free
31+
32+
### Trial
33+
34+
The **"Trial"** edition of the library is fully featured but allows up to 100 assertions (around 25 unit tests) per test project. You may use it in multiple projects to overcome this limitation or just write only a few tests with it (for example asserting a very important route in your application)
35+
36+
- Perfect for previewing the framework
37+
- Completely free with limitations
38+
- Allows up to 100 assertions (around 25 unit tests) per test project
39+
- Developers can include [any package they prefer](/guide/packages.html)
40+
- Fully featured edition
41+
- Can be used by everyone
42+
- The limitation can be removed by providing a license code
43+
44+
### Commercial
45+
46+
The **"Commercial"** edition of the library is fully unlocked and unlimited. Requires a paid license code.
47+
48+
- Perfect for every MVC application
49+
- No limitations
50+
- Requires paid license code
51+
- Developers can include [any package they prefer](/guide/packages.html)
52+
- Fully featured edition
53+
- Can be used by everyone
54+
- License code can be purchased from [HERE](https://mytestedasp.net/Core/Mvc#pricing)
55+
56+
Choose wisely! :)
57+
58+
## Registering a license code
59+
60+
If you followed the tutorial strictly, you should have reached the free trial version limitations of My Tested ASP.NET Core MVC. When you run a single test (no matter which one), it should pass. But if we try to run them all at once, you should receive the following exception:
61+
62+
```
63+
The free-quota limit of 100 assertions per test project has been reached. Please visit https://mytestedasp.net/core/mvc#pricing to request a free license or upgrade to a commercial one.
64+
```
65+
66+
We will now register a license code in **"MusicStore.Test"** and remove this restriction. Create a **"testconfig.json"** file at the root of the project:
67+
68+
<img src="/images/tutorial/testconfigfile.jpg" alt="testconfig.json file at the root of the project" />
69+
70+
Then add the following JSON in it:
71+
72+
```json
73+
{
74+
"License": "1-0GEPhlzJ+jgqzGg+hQPo19wbRvEN0C4LjGm9YDTErbLzcTROsj3fU177Unj7wlOCNE0ciZCB5aw8jt4EEDczpW6S/lW0PkU8ZBjqh6F2ev42hqcgtlEKmBRwomPKj/PUElAo1iIdkLn3/il3o8HAsum7bKMqv7QPpOSwy/TuAGYxOjIwMTctMTAtMTU6YWRtaW5AbXl0ZXN0ZWRhc3AubmV0Ok11c2ljIFN0b3JlIFNhbXBsZSBUZXN0czpEZXZlbG9wZXI6TXVzaWNTdG9yZS4="
75+
}
76+
```
77+
78+
This license is tied to the **"MusicStore.Test"** project. To unlock the testing framework limitations, we need to do one more thing. Go to the **"project.json"** file and add **"testconfig.json"** in the **"copyToOutput"** array:
79+
80+
```json
81+
"buildOptions": {
82+
"preserveCompilationContext": true,
83+
"copyToOutput": [
84+
"config.json",
85+
"testconfig.json"
86+
]
87+
},
88+
```
89+
90+
From now on the **"testconfig.json"** file will be copied to the build output directory and My Tested ASP.NET Core MVC will be able to read it successfully. More information about the test configuration can be found [HERE](/guide/testconfig.html).
91+
92+
Rebuild the solution and run the tests again. All of them should pass. We have unlocked the full version of the library! :)
93+
94+
Additional details about the licensing of the testing framework can be found [HERE](/guide/licensing.html).
95+
96+
## Section summary
97+
98+
Since now we have used the **"Trial"** edition of My Tested ASP.NET Core MVC. It was working fine but we have reached its limitations while still having a lot of tests to write for our web application. After providing a license code in the test project, we are now able to successfully assert with the fully-featured and unlimited **"Community"** edition. Now let's get back to the code and test some [Attributes](/tutorial/attributes/html)!

docs/_docfx/tutorial/options.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Options
21.4 KB
Loading

docs/manifest.json

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

0 commit comments

Comments
 (0)