C# 如何安装 Moq 框架
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13797572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to install Moq framework
提问by Anthony
I plan on using Moq to mock some interfaces in the unit test code I've created. I've already downloaded the latest version of Moq.
我计划使用 Moq 来模拟我创建的单元测试代码中的一些接口。我已经下载了最新版本的 Moq。
My question is how do I install it? Where should I place the Moq.dll?
我的问题是如何安装它?我应该把 Moq.dll 放在哪里?
I've tried searching on the internet, but all I can find are samples of how to use Moq, not how to installit.
我试过在互联网上搜索,但我能找到的只是如何使用 Moq 的示例,而不是如何安装它。
采纳答案by sloth
There's no need to installit. Just add a reference to the moq.dll in your project.
没有必要安装它。只需在您的项目中添加对 moq.dll 的引用。
But of course you can use gacutilto register the library in your global assembly cache.
但是当然您可以使用gacutil在全局程序集缓存中注册该库。
c:\path> gacutil /i Moq.dll
c:\path> gacutil /i Moq.dll
回答by Zdeslav Vojkovic
You don't need to install it.
您不需要安装它。
You could use NuGet of course (if you use newer versions of VS), but you can just copy it to your project folder (or preferably something like libsubdirectory of your project folder) and just add a reference to it.
您当然可以使用 NuGet(如果您使用较新版本的 VS),但您可以将它复制到您的项目文件夹(或者最好是lib项目文件夹的子目录之类的东西),然后添加对它的引用。
EDIT:
编辑:
You seem to have problem with wrong version. In your downloaded moq zip archive, there are multiple folders. You need to use one from folder Net35, not one from Net40. These numbers refer to the version of target .NET framework, not version of Moq itself.
你似乎有错误版本的问题。在您下载的 moq zip 存档中,有多个文件夹。您需要使用一个 from 文件夹Net35,而不是一个 from Net40。这些数字指的是目标 .NET 框架的版本,而不是 Moq 本身的版本。
回答by Sergey Berezovskiy
The best way to add reference to Moq framework is installing it from Nuget. Also you still can downloadMoq.dll and add reference to this library (usually I create folder libsunder the solution folder, where I put all third-party libraries, which is not available via Nuget).
添加对 Moq 框架的引用的最佳方法是从Nuget安装它。此外,您仍然可以下载Moq.dll 并添加对此库的引用(通常我会libs在解决方案文件夹下创建文件夹,我将所有第三方库都放在其中,而 Nuget 无法使用这些库)。
BTWAnother option to install package from Nuget - right click on project references and select Manage Nuget packages.... Then search online for Moq and install it. See why use Nuget over installing libraries directly on my machine
顺便说一句,另一个从 Nuget 安装包的选项 - 右键单击项目引用并选择Manage Nuget packages.... 然后在线搜索 Moq 并安装它。了解为什么使用 Nuget 而不是直接在我的机器上安装库
回答by Jaycee
This is an old question, but the convenient method I used is not listed here and this is the first result on google. I am using VS 2013 and if I search for Moq in Extensions and Updates there are no results so:
这是一个老问题,但是我使用的方便方法这里没有列出,这是google上的第一个结果。我正在使用 VS 2013,如果我在扩展和更新中搜索 Moq,则没有结果,因此:
- Go to Package Manager Console - (Tools -> Library Package Manager)
- Change the default project to your test project
- Then type:
install-package moq
- 转到包管理器控制台 -(工具 -> 库包管理器)
- 将默认项目更改为您的测试项目
- 然后输入:
install-package moq
回答by Rosana Ruiz
When using Visual Studio:
使用 Visual Studio 时:
- Right click on References[It's in the project Explorer]
- Manage NuGet Packages
- Search for Moq and add it to your solution.
- 右键单击References[它在项目资源管理器中]
- 管理 NuGet 包
- 搜索 Moq 并将其添加到您的解决方案中。
回答by CatNinja
If you are on newer version of Visual Studio (2013+), you can use Package Manager Console.
如果您使用的是较新版本的 Visual Studio (2013+),则可以使用包管理器控制台。
Tools > Nuget Package Explorer > Package Manager Console
工具 > Nuget 包资源管理器 > 包管理器控制台
Execute:
执行:
Install-Package Moq -Version 4.5.16
安装包最小起订量 - 版本 4.5.16
Also see: https://www.nuget.org/packages/Moq/
回答by Super Jade
When using Visual Studio
使用 Visual Studio 时
- Right-click the test project in Solution Explorer
- Manage NuGet packages...
- Change Package source to All
- Browse for Moq
- Install
- 在解决方案资源管理器中右键单击测试项目
- 管理 NuGet 包...
- 将包源更改为全部
- 浏览最小起订量
- 安装

