Aspnetcore.docs: Integrations Tests Page Missing Prerequisite

Created on 16 Jun 2018  Â·  28Comments  Â·  Source: dotnet/AspNetCore.Docs

On this page: https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-2.1, under "Test app prerequiresites" the following is listed:

The test project must:
Have a package reference for Microsoft.AspNetCore.App.
Use the Web SDK in the project file (<Project Sdk="Microsoft.NET.Sdk.Web">).

However, WebApplicationFactory is under the Microsoft.AspNetCore.Mvc.Testing package which is not part of the Microsoft.AspNetCore.App metapackage.

Therefore I think there should be mention of needing this package.

Happy to submit a PR for this if it's approved to do so or let a first-timer take this if they want.


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

P2 Source - Docs.ms

Most helpful comment

Microsoft.AspNetCore.Mvc.Testing

This is the only one that you need to add if you start from a test project. (Whether is MSTest or Xunit), I'll leave the rest up to you.

All 28 comments

Thanks @scottsauber.

@javiercn Which ones do you want to call out (or all of them)?

<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />

Additionally, I don't think this part is correct:
Use the Web SDK in the project file (&lt;Project Sdk="Microsoft.NET.Sdk.Web"&gt;).

When I do that and run VS yells about not having a Main() method and @javiercn isn't using that on the IdentityUI tests here: https://github.com/aspnet/Identity/blob/ae9aa9ebec4de42d2c0ab8c68f1b2bccd7b1c5f8/test/Identity.FunctionalTests/Microsoft.AspNetCore.Identity.FunctionalTests.csproj#L1

My tests do run fine though if my test project is just Microsoft.NET.Sdk

That's what I had in the PR originally. It worked for me, too, during the early PR development ...

https://github.com/aspnet/Docs/pull/6086/commits/55cfaf0e6aa2de8e091512b9924437493dd53aa8#diff-18046397f76a4af005492bd1aad36ae1

... but I was told to change it to the Web SDK. I don't see the thread tho where it was discussed ... it may be buried in an email exchange. @javiercn will clear it up.

Edit on 6/21/18: Nevermind - I can't repro this now, so I'm removing this comment to improve clarity of the outstanding issues. I must've screwed something up.

:smile: I had something very similar to what you show in the original integration testing topic (before the new Test package came out and we did the topic updates), but I couldn't get that approach to work with the new bits, so I went with what you see in the topic, and it works here. I just pulled the topic's sample down and ran it ...

capture

When I try what you posted, I get the opposite ... closer to your error ... I think that's exactly what the original code was doing when I started working on the PR.

System.InvalidOperationException : No service for type 'RazorPagesProject.Data.ApplicationDbContext' has been registered.

This is another one that @javiercn might be able to sort out.

Weird. Traveling right now, but I’ll try to do a minimal repro tonight and if I can, I’ll push a repo with the repro (/end my rhyming ability).

Hmm... for whatever reason, on a brand new Razor Pages project, I can get both the approach in this article AND my approach to work.

The plot thickens....

You can disregard the CustomWebApplicationFactory. I can get the one in the docs to work now... so clearly user error on my end of something I was doing. I edited out my comment to reduce confusion for anyone coming in later to read this bc it was a wall of text.

So I think the outstanding issues are:

  • Add a mention of the Microsoft.AspNetCore.Mvc.Testing package under the pre-reqs.
  • Switch the project file from Microsoft.NET.Sdk.Web to Microsoft.NET.Sdk

This one ...

Switch the project file from Microsoft.NET.Sdk.Web to Microsoft.NET.Sdk

... I don't think is going to happen b/c it was explicitly called for by engineering. @javiercn will need to remind me the reason.

This one ...

Add a mention of the Microsoft.AspNetCore.Mvc.Testing package under the pre-reqs.

... we should list them all if we'll call it out ...

  • Microsoft.AspNetCore.Mvc.Testing
  • Microsoft.NET.Test.Sdk
  • xunit
  • xunit.runner.visualstudio

... @javiercn, do u want to do that?

I'll be curious to hear the reason, because I can't .Web to work then and it's not on Identity's Functional Tests.

IMO it'd be more clear to itemize all the packages required like you say, considering another package, .App is called out.

Web is probably required because the App metapackage is called for (from https://docs.microsoft.com/en-us/aspnet/core/fundamentals/metapackage-app?view=aspnetcore-2.1) ...

must be set to Microsoft.NET.Sdk.Web to use the implicit version Microsoft.AspNetCore.App. When is used, the following warnings are generated:

Warning NU1604: Project dependency Microsoft.AspNetCore.App does not contain an inclusive lower bound. Include a lower bound in the dependency version to ensure consistent restore results.

Warning NU1602: [Project Name] does not provide an inclusive lower bound for dependency Microsoft.AspNetCore.App. An approximate best match of Microsoft.AspNetCore.App 2.1.0 was resolved.

I think if you set the App package version explicitly, then you might be able to get away with only Microsoft.NET.Sdk.

Ah you're right. When I omitted the version number, then .Web works.

I may either point that out directly or link over to the App metapackage page where it's discussed.

I'm going to go ahead and put something together to deal with both of these situations soon ... probably tomorrow.

Sounds good. While you're in there - prerequisites is spelled incorrectly here:

These prerequesities can be seen in the sample app.

Thanks. I'm surprised that one slipped. This topic was spell checked. That was a sneaky word. 😈

If you look... I edited my post pointing it out because I misspelled it the first time. It's a tough one for sure.

The reference to Microsoft.Net.Sdk.Web is required because how microsoft.aspnetcore.app works. Identity gets away without it because it directly references the MVC package. There is some SDK magic regarding references going on, but the safe thing to do is to reference the SDK.

The Microsoft.AspNetCore.Mvc.Testing package needs to be referenced separately as its not part of the metapackage, but that's it.

@javiercn Do you want me to call out these package requirements explicitly in the topic prereqs? It seems like a reasonable action to take.

  • Microsoft.AspNetCore.Mvc.Testing
  • Microsoft.NET.Test.Sdk
  • xunit
  • xunit.runner.visualstudio

Microsoft.AspNetCore.Mvc.Testing

This is the only one that you need to add if you start from a test project. (Whether is MSTest or Xunit), I'll leave the rest up to you.

I have all these references in my integration test project:

<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.1.0" /> <PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.0" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0" /> <PackageReference Include="Shouldly" Version="3.0.0" /> <PackageReference Include="xunit" Version="2.3.1" /> <PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" /> <DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />

But when I try to use the CustomWebApplicationFactory and run a test I get the following error:

System.InvalidOperationException : Can't find'C:UsersmathesourcereposnewIdentityServertestsMSAP.IntegrationTestbinDebugnetcoreapp2.1Microsoft.AspNetCore.Mvc.Testing.deps.json'. This file is required for functional tests to run properly. There should be a copy of the file on your source project bin folder. If that is not the case, make sure that the property PreserveCompilationContext is set to true on your project file. E.g 'true'. For functional tests to work they need to either run from the build output folder or the Microsoft.AspNetCore.Mvc.Testing.deps.json file from your application's output directory must be copied to the folder where the tests are running on. A common cause for this error is having shadow copying enabled when the tests run.
at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory1.EnsureDepsFile() at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory1.EnsureServer()
at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory1.CreateDefaultClient(DelegatingHandler[] handlers) at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory1.CreateClient(WebApplicationFactoryClientOptions options)
at MSAP.IntegrationTests.ApiClientControllerShould..ctor(CustomWebApplicationFactory`1 factory) in C:UsersmathesourcereposnewIdentityServertestsMSAP.IntegrationTestApiClientControllerShould.cs:line 20

Hello @matheus06 ... You'll probably get a faster answer on Stack Overflow. Here on the docs repo, we're not really staffed to field support requests. Certainly if you come to find that we're missing something from the doc that would have saved you, please do let us know about that.

One quick thing since the error calls it out. You did disable shadow copying, correct?

@guardrex - prerequisites is still misspelled as prerequesities

DAH! :smile:

I must have looked at the wrong word.

Yeah ... I was looking at the heading ... not the text. Thanks for catching that @scottsauber! :rocket:

btw tho ...... I'm submitting prerequesities to the Urban Dictionary. However, I've HATED on the Urban Dictionary ever since they robbed me of credit for coining the term ...

slackerdaisical

I invented that term over ten years ago ... submitted it to them ... they rejected it ... now they approved it a short time ago and gave someone else credit! Jerks! lol

Lol. FTR - I couldn't spell it either 15 mins ago when I commented. I spelled it prequisities at first. Good thing Chrome is spell checking me. :)

Slackerdaiscal is awesome.

Was this page helpful?
0 / 5 - 0 ratings