Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

ASP.Net Core 3 ContentRoot?
BrianAdams
#1 Posted : Monday, January 6, 2020 1:51:50 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 4/7/2016(UTC)
Posts: 8
Location: United States of America

Was thanked: 2 time(s) in 2 post(s)
I have a test project that uses the ASP.NET Core TestServer to host the web project for testing. I've recently upgraded from Asp.NET Core 2.2 targeting net48 to an Asp.NET 3.1 targeting netcoreapp3.1. Previously, NCrunch would copy both the Web Project and the Test project folders into the temp location where NCrunch runs tests.

It looked something like this
AppData\Local\NCrunch\47380\83\TESTPROJECT
AppData\Local\NCrunch\47380\83\WEBPROJECT

This meant that in my TestStartup class I could walk up the folders until I found the folder containing the web project (and ultimately wwwroot). Now, after the upgrade, the web project is no longer copied into the same temp folder as the test project. This means I can no longer find the web project's folder and set the ContentRoot and WwwRoot folder locations. All of my tests now fail because of this.

Is there an article somewhere illustrating how to test an Asp.NET Core 3.X project with NCrunch? The built-in VS Test Runner works fine, so I know it's not my tests, but instead is the way NCrunch copies things to the temp folder.
michaelkroes
#2 Posted : Monday, January 6, 2020 6:43:36 PM(UTC)
Rank: NCrunch Developer

Groups: Registered
Joined: 9/22/2017(UTC)
Posts: 200
Location: Netherlands

Thanks: 87 times
Was thanked: 41 time(s) in 39 post(s)
Hi,

Thanks for posting!

Are you using Microsoft.AspNetCore.Mvc.Testing?

I'm not an expert with that package, but this example works for me:

Code:

			var server = new TestServer(new WebHostBuilder()
				.UseContentRoot(Directory.GetCurrentDirectory())
				.UseEnvironment("Development")
				.UseStartup(typeof(Startup)));

			var client = server.CreateClient();
			var res = await client.GetAsync("/api/values");

			var content = await res.Content.ReadAsStringAsync();


Nothing special was done in the NCrunch configuration for this. The api project is the default api example.

If your setup requires something more intricate, maybe this will help: https://www.ncrunch.net/...tional-files-to-include

I'm not sure if this help, let us know if it doesn't :)
BrianAdams
#3 Posted : Monday, January 6, 2020 9:16:54 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 4/7/2016(UTC)
Posts: 8
Location: United States of America

Was thanked: 2 time(s) in 2 post(s)
I am using Microsoft.AspNetCore.Mvc.Testing but no, unfortunately, that doesn't work for me. The structure I'm getting now is this:

AppData\Local\NCrunch\47380\82\WEBPROJECT
AppData\Local\NCrunch\47380\83\TESTPROJECT

Notice WEBPROJECT is under subfolder named 82 while TESTPROJECT is under 83...

Directory.GetCurentDirectory() gives me this:

AppData\Local\NCrunch\47380\83\TESTPROJECT\bin\Debug\netcoreapp3.1\

when what I actually need for content root is the location under the WEBPROJECT. For now, I just walk up directories until I get to the test project's root folder. Then, I go one more and convert the name of the folder into an int. From there I start subtracting (or adding) 1 to the name, and search for the web folder. Once I find it, I set it as the content root. This works, but OH MAN does it feel fragile and unnecessary. I must be missing something.
Remco
#4 Posted : Tuesday, January 7, 2020 12:11:03 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 5,921

Thanks: 773 times
Was thanked: 990 time(s) in 943 post(s)
Hi Brian,

I'm not sure why this has started happening for you in the new version of NCrunch. I wasn't aware that we'd changed anything here.

NCrunch always places projects in different numerical paths under the workspace root. The best way to find a project is to actually use the location of its output binary by first calling NCrunchEnvironment.GetAllAssemblyLocations then grabbing the first assembly listed with a name matching the output file of the web project. Assuming this would be under bin\debug, you could then navigate two levels up from the output assembly's directory and you'd have your web project's location without needing any tricky inference or additional duct-tape.
BrianAdams
#5 Posted : Tuesday, January 7, 2020 12:21:39 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 4/7/2016(UTC)
Posts: 8
Location: United States of America

Was thanked: 2 time(s) in 2 post(s)
Ahh, I didn't know about the GetAllAssemblyLocations method, brilliant!

FWIW, it's not the new version of NCrunch that made this start happening. I can run a different branch of the same solution that hasn't been upgraded to Asp.NET Core 3.1 and it works fine. The solution is a bit odd in that it has two websites and about 40 class library projects.

One website is new and based on .net core and the other is legacy and based on classic asp.net MVC 4. They both depended on EF 6, so we were targeting the NET Framework with both websites and all class libraries. With NETStandard 2.1 now supporting EF 6, we went ahead an updated all the class libraries to multi-target netstandard2.1 and net48. The old asp.net project remained as it was, and the new asp.net core project got upgraded to netcoreapp3.1.

This works great, but for some reason NCrunch started behaving differently. I think we are just confusing it though. We have a shared test framework class library that multi-targets, and two other test projects.
One that exercises the legacy site which is still using net48 target and another that exercises the asp.net core site and it targets netcoreapp3.1. The one that targets the net core site is the one that started acting differently.

Thanks for your help!
1 user thanked BrianAdams for this useful post.
Remco on 1/7/2020(UTC)
hhoangnl
#6 Posted : Tuesday, January 7, 2020 10:47:13 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 10/27/2017(UTC)
Posts: 1
Location: Netherlands

I have a similar issue. Upgrading a project from 2.2 to 3.1 causes all TestServer related unit tests to fail.
However, they succeed in ReSharper's unit test tool.

Error 1:
Quote:
System.InvalidOperationException: The view 'Y' was not found. The following locations were searched:
/Views/X/Y.cshtml
/Views/Shared/Y.cshtml
/Pages/Shared/Y.cshtml


The other error is that every endpoint that is being called by the unit test results in a 404 when I expect other HTTP codes.
Remco
#7 Posted : Wednesday, January 8, 2020 12:11:15 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 5,921

Thanks: 773 times
Was thanked: 990 time(s) in 943 post(s)
hhoangnl;14317 wrote:
I have a similar issue. Upgrading a project from 2.2 to 3.1 causes all TestServer related unit tests to fail.
However, they succeed in ReSharper's unit test tool.


Hi, thanks for sharing this problem. I'm having some difficulty reproducing it. Are you able to build a sample solution that can reproduce this problem and you can share with me? You can submit code in ZIP form through the NCrunch contact form.
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.062 seconds.