Adding MOQ to a .NET 4.0 project is not possible

2019-04-22 19:11发布

问题:

I want to add MOQ .net library to a .NET 4.0 project. I get an error message that I should convert the project to something smaller than 4.0. I need to use .NET 4.0 also because of other project dependencies.

How can I make MOQ work with a .NET 4.0 project in VS2010 ?

回答1:

  • Make sure you have NuGet package manager installed (Tools/Extension Manager).

  • Right click on Solution Explorer/References, choose Manage NuGet Packages.

  • Type Moq in the search box.

  • Install and enjoy.



回答2:

The latest version from Google Code (http://code.google.com/p/moq/), works in a .Net 4.0 project. I would just download the dll again.

Here is the download link: http://code.google.com/p/moq/downloads/detail?name=Moq.4.0.10827.Final.zip

The zip file at that location contains a .Net 4 folder. Use the dll : Moq.dll from the NET40 folder and everthing will work fine. Just do a normal Add Reference on the project.

I would check the version number and the Runtime veseion of the assembly you are trying to use. It should say:

Runtime Version: v4.0.20926

Version: 4.0.10827.0



回答3:

For the sake of completeness, it may be worth pointing out that it is also possible to use the Visual Studio command line package manager tool:

1) Open the console in VS

View >> Other Windows >> Package Manager Console

2) Install Moq

Type Install-Package Moq at the prompt

Note: If your tests are in a separate project, make sure that you select that project in the project dropdown.

This will install the latest package available in NuGet, which should address your compatibility issue. See http://nuget.org/packages/moq.



回答4:

Watch out that you're using .NET 4, rather than the .NET 4 Client Profile as your Target Framework. I had the same issue and that was the cause for me.