20

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.

My question is how do I install it? Where should I place the Moq.dll?

I've tried searching on the internet, but all I can find are samples of how to use Moq, not how to install it.

Super Jade
  • 5,609
  • 7
  • 39
  • 61
Anthony
  • 437
  • 2
  • 8
  • 18
  • What version of Visual Studio are you using? 2010? Try to install it using nuget package manager - see this post: http://stackoverflow.com/a/9897738/558486 – Rui Jarimba Dec 10 '12 at 08:50
  • I'm using Visual Studio 2008. I don't have VS2010 or VS2012. – Anthony Dec 10 '12 at 08:58
  • 1
    You need to add a reference to the DLL. See: http://msdn.microsoft.com/en-us/library/wkze6zky(v=vs.90).aspx – Rui Jarimba Dec 10 '12 at 09:06
  • Refer Refer [To install Moq: an enjoyable mocking library](http://www.nuget.org/packages/MOQ) and [Finding and Installing a NuGet Package Using the Package Manager Console](http://docs.nuget.org/docs/start-here/using-the-package-manager-console) – LCJ Feb 03 '14 at 10:28
  • Also refer [Adding MOQ to a .NET 4.0 project](http://stackoverflow.com/questions/9897564/adding-moq-to-a-net-4-0-project-is-not-possible) – LCJ Feb 03 '14 at 10:48

8 Answers8

24

The best way to add reference to Moq framework is installing it from Nuget. Also you still can download Moq.dll and add reference to this library (usually I create folder libs under the solution folder, where I put all third-party libraries, which is not available via Nuget).

BTW Another 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

Community
  • 1
  • 1
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • does NuGet support VS2008? from what I'm seeing, it seems that it doesn't. – Anthony Dec 10 '12 at 09:27
  • @Anthony well, you can [add support of Nuget](http://www.hanselman.com/blog/NuGetSupportForVisualStudio2008.aspx) but if it's possible, consider moving to VS2010 or VS2012 – Sergey Berezovskiy Dec 10 '12 at 09:43
  • Where can I download a recent version of the DLL, @SergeyBerezovskiy? Since they moved to Github, http://code.google.com/p/moq/downloads/list seems not to be updated (and my current environment prevents me from using Nuget). – chesterbr Mar 11 '14 at 14:23
  • 1
    Ah, nevermind, found a copy on the downloadable repo here: https://github.com/Moq/moq4/releases – chesterbr Mar 11 '14 at 14:25
9

There's no need to install it. Just add a reference to the moq.dll in your project.

But of course you can use gacutil to register the library in your global assembly cache.

c:\path> gacutil /i Moq.dll

sloth
  • 99,095
  • 21
  • 171
  • 219
  • I've added the .dll to a folder within my project now, and referenced it in the project. my problem now is that, somehow, I can't use it. In the sense that the `Mock` class doesn't appear. Did I forget to do something? – Anthony Dec 10 '12 at 09:19
  • Have you imported the namespace? You usually can just use the class, like `var mock = new Mock();`, and then let Visual Studio import the right namespace on its own by pressing `CTRL` + `.`. – sloth Dec 10 '12 at 09:24
  • can you show me how it's done? what with importing the namespace and all. thanks :) – Anthony Dec 10 '12 at 09:33
  • Just add `using Moq;` to the beginning of your source file. – sloth Dec 10 '12 at 09:38
9

When using Visual Studio:

  1. Right click on References [It's in the project Explorer]
  2. Manage NuGet Packages
  3. Search for Moq and add it to your solution.
Rosana Rufer
  • 599
  • 8
  • 15
6

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:

  1. Go to Package Manager Console - (Tools -> Library Package Manager)
  2. Change the default project to your test project
  3. Then type: install-package moq
Jaycee
  • 3,098
  • 22
  • 31
4

For .NET Core, using the dotnet CLI - dotnet add package Moq

Chris Halcrow
  • 28,994
  • 18
  • 176
  • 206
1

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 lib subdirectory of your project folder) and just add a reference to it.

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.

Zdeslav Vojkovic
  • 14,391
  • 32
  • 45
  • I did that before, but it says that it can't load the file. `"This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded."` By the way, I am using Moq 4.0 on VS2008. – Anthony Dec 10 '12 at 09:03
  • @Anthony It seems you are trying to load the .Net 4.0 version of Moq from an assembly targeting .Net 3.5 (or older). Use the .Net 3.5 version of Moq, then. – sloth Dec 10 '12 at 09:08
1

When using Visual Studio

  1. Right-click the test project in Solution Explorer
  2. Manage NuGet packages...
  3. Change Package source to All
  4. Browse for Moq
  5. Install
Super Jade
  • 5,609
  • 7
  • 39
  • 61
0

If you are on newer version of Visual Studio (2013+), you can use Package Manager Console.

Tools > Nuget Package Explorer > Package Manager Console

Execute:

Install-Package Moq -Version 4.5.16

Also see: https://www.nuget.org/packages/Moq/

CatNinja
  • 109
  • 1
  • 9