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
Copy file name to clipboardExpand all lines: docs/_docfx/tutorial/gettingstarted.md
+87-1Lines changed: 87 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -106,4 +106,90 @@ You still here? Good! Now repeat after me and then everything will be explained
106
106
107
107
Go to the **"MusicStore"** project root, copy the **"config.json"** file and paste it at the root of the test project.
108
108
109
-
// Make screenshot directly on the project.json with highlighted the two files, remove the database connection string
109
+
Go to the **"project.json"** file at the test project and add the copied **"config.json"** file to the build output:
110
+
111
+
```json
112
+
"buildOptions": {
113
+
"copyToOutput": [
114
+
"config.json"
115
+
]
116
+
},
117
+
```
118
+
119
+
<imgsrc="/images/tutorial/configjson.jpg"alt="Copied config.json from the web project" />
120
+
121
+
You may be a bit worried about the connection string in the **"config.json"** file. I would be, if I did not know it was not needed at all. You can change it to whatever you like so let's make it "Test Connection". If you want to feel even safer, you may change all the other options too. It's up to you.
122
+
123
+
Your **"config.json"** file should look like this:
Now run the test again in Visual Studio and... oh, miracle, it passes! :)
142
+
143
+
Don't be too happy yet as there is a (not-so) small problem here. Visual Studio runs the discovered tests only for the first targeted framework, which in our case is **"netcoreapp1.0"**. But how to test for the other one - **"net451"**?
144
+
145
+
Go to the **"MusicStore.Test"** project folder and open a console terminal there. The easiest way is pressing "SHIFT + Right Mouse Button" somewhere in the window and then clicking "Open command window here".
146
+
147
+
<imgsrc="/images/tutorial/terminal.jpg"alt="Console terminal at the root of the test project" />
148
+
149
+
Type **"dotnet test"** and hit **"Enter**".
150
+
151
+
<imgsrc="/images/tutorial/net451fail.jpg"alt=".NET Framework test fails" />
152
+
153
+
Oh, well... This does not look good... Fail again! Why are you even doing this tutorial, you may wonder? :(
154
+
155
+
If you still decide to stick around - .NET Core runs our test fine but the full .NET framework fails because it cannot load correctly all the plugin dependencies My Tested ASP.NET Core MVC needs (more information available [HERE](/guide/plugins.html).
156
+
157
+
Do not worry, this one is easy. Go to the test assembly's **"project.json"** file and set the **"preserveCompilationContext"** option under **"buildOptions"** to **"true"**:
158
+
159
+
```json
160
+
"buildOptions": {
161
+
"preserveCompilationContext": true,
162
+
"copyToOutput": [
163
+
"config.json"
164
+
]
165
+
},
166
+
```
167
+
168
+
Go back to the console terminal and run **"dotnet test"** again.
169
+
170
+
<imgsrc="/images/tutorial/firsttestpass.jpg"alt="First test passes" />
171
+
172
+
Oh, miracles! The test passes correctly without any loud and ugly errors! Oh, yeah, do you feel the happiness? This library is really DA BOMB!!! :)
173
+
174
+
OK, back to that promise - the detailed explanation of all the fails. **Basically three things happened.**
175
+
176
+
**First**, My Tested ASP.NET Core MVC needs to resolve the services required by the different components in your web application - controllers, view components, etc. By default the test configuration needs a **"TestStartup"** class at the root of the test project from where it configures the global service provider. This is why we got an exception telling us we forgot to add it. Remember:
177
+
178
+
- Each test project needs separate **"Startup"** class and bootstraps a separate test application and service provider. You may run different configurations in different test assemblies.
179
+
- Each test is run in a scoped service lifetime - during a test all scoped services will be resolved by using the same instance and for the next test, another instance will be provided. My Tested ASP.NET Core MVC uses this nice little feature to allow for easy and autonomous testing of storage providers like the **"DbContext"**, **"IMemoryCache"**, **"ViewDataDictionary"** and many more but more on that later in the tutorial.
180
+
181
+
Besides the default **"TestStartup"** configuration there are two other options the developer can use - fluent manual configuration and per test setup without any globally registered services. More information can be found [HERE](/guide/startuptypes.html).
182
+
183
+
**Second**, the test failed because we did not have the required **"config.json"** file. If you take a look at the **"Startup"** file in the web project, you may see that the constructor of the class has the following lines of code:
184
+
185
+
```c#
186
+
varbuilder=newConfigurationBuilder()
187
+
.SetBasePath(hostingEnvironment.ContentRootPath)
188
+
.AddJsonFile("config.json")
189
+
```
190
+
191
+
TheJSONfileisnotoptionalandsinceweinheritfromtheoriginalweb **"Startup"**, our**"TestStartup"**classruns the same code thus requiring the **"config.json"** file to be present. (Un)fortunately the base project directory will be the output directory of the test project and the test runner will search for the file there. While we may make the **"config.json"** optional, it may lead to unexpected behaviour and exceptions so our best option here is to copy the same file into the test project and change all important values with dummy ones. Copy-pasting is not a good practice but letting the tests touch and read the original application configuration values like database connection strings, security passwords and potentially others is even worse. Additionally, only copying the file is not enough for it to end up in the output directory so we need to add it explicitly in the test assembly's **"project.json"** configuration.
192
+
193
+
**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.
194
+
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! :)
Copy file name to clipboardExpand all lines: docs/api/MyTested.AspNetCore.Mvc.ActionAttributesTestBuilderViewFeaturesExtensions.html
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -87,7 +87,7 @@ <h3 id="methods">Methods
87
87
</h3>
88
88
<spanclass="small pull-right mobile-hide">
89
89
<spanclass="divider">|</span>
90
-
<ahref="https://github.com/ivaylokenov/MyTested.AspNetCore.Mvc/new/development/docs/_docfx/apispec/new?filename=MyTested_AspNetCore_Mvc_ActionAttributesTestBuilderViewFeaturesExtensions_ValidatingAntiForgeryToken_MyTested_AspNetCore_Mvc_Builders_Contracts_Attributes_IActionAttributesTestBuilder_.md&value=---%0Auid%3A%20MyTested.AspNetCore.Mvc.ActionAttributesTestBuilderViewFeaturesExtensions.ValidatingAntiForgeryToken(MyTested.AspNetCore.Mvc.Builders.Contracts.Attributes.IActionAttributesTestBuilder)%0Asummary%3A%20'*You%20can%20override%20summary%20for%20the%20API%20here%20using%20*MARKDOWN*%20syntax'%0A---%0A%0A*Please%20type%20below%20more%20information%20about%20this%20API%3A*%0A%0A">Improve this Doc</a>
90
+
<ahref="https://github.com/ivaylokenov/MyTested.AspNetCore.Mvc/new/tutorial/docs/_docfx/apispec/new?filename=MyTested_AspNetCore_Mvc_ActionAttributesTestBuilderViewFeaturesExtensions_ValidatingAntiForgeryToken_MyTested_AspNetCore_Mvc_Builders_Contracts_Attributes_IActionAttributesTestBuilder_.md&value=---%0Auid%3A%20MyTested.AspNetCore.Mvc.ActionAttributesTestBuilderViewFeaturesExtensions.ValidatingAntiForgeryToken(MyTested.AspNetCore.Mvc.Builders.Contracts.Attributes.IActionAttributesTestBuilder)%0Asummary%3A%20'*You%20can%20override%20summary%20for%20the%20API%20here%20using%20*MARKDOWN*%20syntax'%0A---%0A%0A*Please%20type%20below%20more%20information%20about%20this%20API%3A*%0A%0A">Improve this Doc</a>
<ahref="https://github.com/ivaylokenov/MyTested.AspNetCore.Mvc/new/development/docs/_docfx/apispec/new?filename=MyTested_AspNetCore_Mvc_ActionAttributesTestBuilderViewFeaturesExtensions.md&value=---%0Auid%3A%20MyTested.AspNetCore.Mvc.ActionAttributesTestBuilderViewFeaturesExtensions%0Asummary%3A%20'*You%20can%20override%20summary%20for%20the%20API%20here%20using%20*MARKDOWN*%20syntax'%0A---%0A%0A*Please%20type%20below%20more%20information%20about%20this%20API%3A*%0A%0A" class="contribution-link">Improve this Doc</a>
145
+
<ahref="https://github.com/ivaylokenov/MyTested.AspNetCore.Mvc/new/tutorial/docs/_docfx/apispec/new?filename=MyTested_AspNetCore_Mvc_ActionAttributesTestBuilderViewFeaturesExtensions.md&value=---%0Auid%3A%20MyTested.AspNetCore.Mvc.ActionAttributesTestBuilderViewFeaturesExtensions%0Asummary%3A%20'*You%20can%20override%20summary%20for%20the%20API%20here%20using%20*MARKDOWN*%20syntax'%0A---%0A%0A*Please%20type%20below%20more%20information%20about%20this%20API%3A*%0A%0A" class="contribution-link">Improve this Doc</a>
0 commit comments