通过本地项目引用导入 NuGet 引用 [英] Importing NuGet references through a local project reference

查看:66
本文介绍了通过本地项目引用导入 NuGet 引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个主"C# 项目,它使用 NuGet 来管理其第三方依赖项.

Let's say I have a "main" C# project that uses NuGet to manage its third-party dependencies.

现在假设我在主项目旁边创建了一个单元测试项目,其中包含主项目作为参考.

Now let's say I create a unit-testing project alongside the main project that includes the main project as a reference.

不幸的是,我似乎需要在主项目中重新添加通过 nuget 包含的依赖项,以便使用它们为单元测试项目中的单元测试编写代码.

Unfortunately, it seems that I need to re-add dependencies that are included via nuget in the main project in order to use them to write code for the unit tests in the unit test project.

我的问题是:有没有办法在测试项目中自动包含主要"nuget 引用?

My question is: Is there a way to automatically include the "main" nuget references in the testing project?

推荐答案

看来我需要在主项目中重新添加通过nuget包含的依赖项,以便使用它们为单元测试项目中的单元测试编写代码.

it seems that I need to re-add dependencies that are included via nuget in the main project in order to use them to write code for the unit tests in the unit test project.

那是因为 Visual Studio/MSBuild 试图变得聪明,并且只将引用带入项目单元测试中,它检测到项目main"需要它.这样做是为了避免项目单元测试中的间接引用污染.您可以参考接受的答案 了解更多详情.

That because Visual Studio / MSBuild tries to be smart and only bring references over into project unit-testing that it detects as being required by project "main". It does this to avoid indirect reference pollution in project unit-testing. You can refer the accepted answer for more detail.

我的问题是:有没有办法在测试项目中自动包含主要"nuget 引用?

My question is: Is there a way to automatically include the "main" nuget references in the testing project?

简单的答案是肯定的,但不确定此方法是否对您有效.我把它贴在这里,希望对你有所帮助.

The simple answer is yes, but not sure whether this method is effective for you. I post it here, hope it can give you some help.

您可以将要添加到 UI-testing 的包列表从主"项目中的 package.config复制到 UI-testing 项目中的 package.config,例如:

You can copy the package list which you want to add to UI-testing from package.config in the "main" project to the package.config in the UI-testing project, like:

  <package id="EntityFramework" version="6.1.3" targetFramework="net452" />
  <package id="Newtonsoft.Json" version="10.0.3" targetFramework="net452" />
  <package id="NUnit" version="3.7.1" targetFramework="net452" />

然后nuget会在你构建UI测试的时候恢复那些包,你需要做的是在包管理器控制台中使用NuGet命令行:

Then nuget will restore those packages when you build the UI-testing, what you need to do is use the NuGet command line in the Package Manager Console:

Update-Package -Reinstall -ProjectName [name of your target project]

强制将包引用重新安装到 UI 测试项目中.

to force reinstall the package references into UI-testing project.

这篇关于通过本地项目引用导入 NuGet 引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆