How do you unit test a basic ASP.NET web application (.NET Framework)?

I am building a basic ASP.NET web application (.NET Framework) and it is difficult for me to determine how to connect unit tests to it. I am targeting the .NET Framework version 4.6.1

If I create a regular “Class Library” project oriented to 4.6.1, as in the previous version of MVC, it allows me to add links to my MVC project (which is part of the same solution), but any namespaces that I add through the operator using report an error due to which there may be no link or using statement. If I double-clicked the link in the "Links" section of the solution explorer, it will inform me that the project cannot be found or has not yet been created.

I tried creating a "Class Library (.NET Core)," but that complains, as I am targeting the .NET Framework, not the .NET Core. I edited the libaries classes of Project.json so that it targets the .net framework, and this allows me to add links and does not complain when I take up space in the using statement, but none of my tests are detected by the test runner. I tried both XUnit and NUnit and they both behave the same.

Is it possible to unit test ASP.Net Core web application to be guided by .NET Framework 4.6.1, or I need to fix .NET Core?

Edit to add my test class. Here is my test class, divided to a minimum. TestBank.Services is the class I want to test.

 using System; using TestBank.Services; using Xunit; namespace TestBankUnitTests { public class Class1 { [Fact] public void TestA() { Assert.True(false); } } } 

and here is my .json project

 { "version": "1.0.0-*", "dependencies": { "xunit": "2.1.0", "dotnet-test-xunit": "1.0.0-rc2-build10025", "TestBank": "1.0.0-*" }, "frameworks": { "net461": { } } } 
+6
source share
3 answers

Your project.json requires the installation of testRunner . In the project.json documentation, the testRunner parameter not only determines which test environment to use, but also marks the project as a test project.

Try adding it and see if it finds your tests (checked locally that it will not find tests without this setting in the file)

 { "version": "1.0.0-*", "dependencies": { "xunit": "2.1.0", "dotnet-test-xunit": "1.0.0-rc2-build10025", "TestBank": "1.0.0-*" }, "frameworks": { "net461": { } }, "testRunner": "xunit" } 
+8
source

I had a similar problem and found a solution. I post it here if it helps someone. What I found out is that using net461-oriented xproj library to test the Asp.Net Core Project (.Net Framework) with Net461 targeting and the 4.1 class targeting platform works, but the setup seems very thin and fragile. The key insight for me came from this thread https://github.com/aspnet/Tooling/issues/245 , where @BradRem pointed out that the project folder structure seems to be the source of the problems.

Initially, I tried unsuccessfully to use this folder structure:

CSI
____ Project Net Project Project Core Project (.Net Framework)
____ Advertising platform for the Windows 4.61 class library

test
____Core library used to run xUnit tests

But when I tried to run the tests using this folder structure, he threw an exception that starts as follows:

 Unable to start C:\Program Files\dotnet\dotnet.exe dotnet-test Error: 0 : [ReportingChannel]: Waiting for message failed System.IO.IOException: Unable to read data from the transport connection: An established connection was aborted by the software in your host machine. ---> System.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) --- End of inner exception stack trace --- at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) at System.IO.Stream.ReadByte() at System.IO.BinaryReader.ReadByte() at System.IO.BinaryReader.Read7BitEncodedInt() at System.IO.BinaryReader.ReadString() at Microsoft.DotNet.Tools.Test.ReportingChannel.ReadMessages() 

But when I switched to the following folder structure, I was able to make it work:

CSI
____ Project Net Project Project Core Project (.Net Framework)
____ Size for targeting the Windows 4.61 class library
____Core library used to run xUnit tests

Thus, the key was placed in the folder in which the library of testing classes was located, in the src folder, where other project folders were located.

However, another thing that seemed to matter a lot was to add links to two other projects to the Core Library test project at the same time, and not one at a time.

0
source

I found something that works for me.
In my test project, my project.json file looks like this:

 { "version": "1.0.0-*", "testRunner": "xunit", "dependencies": { "xunit": "2.2.0-beta2-build3300", "dotnet-test-xunit": "2.2.0-preview2-build1029", "Ng2a.WebApi": "1.0.0-*" }, "frameworks": { "net452": { } } } 

'Ng2a.WebApi' is my main api project. It is intended for net452 only. The project.json Ng2a.WebApi file is as follows:

 "frameworks": { "net452": {} 

},

0
source

All Articles