Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking framework for asp.net core 5.0

I recently installed Visual Studio 2015 and started a project with a web site and a asp class library which will contain the unit tests for the web site. I usually use Moq for mocking but I am no stranger to try a different mocking framework. The problem I am having is that I added Moq as a reference to the unit test project and started using it. Everything seems fine at first until I tried to compile.

When I compiled I got an error message saying:

ASP.NET Core 5.0 error CS0246: The type or namespace name 'Moq' could not be found (are you missing a using directive or an assembly reference?)

I noticed that I could switch between ASP.NET 5.0 and ASP.NET Core 5.0 in the code view and when selecting ASP.NET Core 5.0 I get errors but no when selecting ASP.NET 5.0. I tried searching for an answer but I did not have any luck.

Is the problem that Moq does not work with vnext and I should use a different framework (if so which works?) or can I solve this somehow? My project.json:

{ "version": "1.0.0-*", "dependencies": {     "Web": "1.0.0-*",     "Xunit.KRunner": "1.0.0-beta1",     "xunit": "2.0.0-beta5-build2785",     "Moq": "4.2.1409.1722" },  "frameworks": {     "aspnet50": {         "dependencies": {         }     },     "aspnetcore50": {         "dependencies": {         }     } }, "commands": {     "test": "Xunit.KRunner" }  } 
like image 264
user1842278 Avatar asked Jan 13 '15 09:01

user1842278


People also ask

Does MOQ support .NET 5?

Moq is a library that allows us to create mock objects in test code. It is also available in NuGet. This library also supports . NET Core.

What is mocking framework C#?

Mocking Frameworks (Moq, NSubstitute, Rhino Mocks, FakeItEasy, and NMock3) are used to create fake objects. We can stub, i.e., completely replace the body of member and function. It is used to isolate each dependency and help developers in performing unit testing in a concise, quick, and reliable way.

What are mocking frameworks?

What is a mocking framework? Mocking frameworks are used to generate replacement objects like Stubs and Mocks. Mocking frameworks complement unit testing frameworks by isolating dependencies but are not substitutes for unit testing frameworks.

Is MOQ a framework in C#?

Quick glance at Moq - Unit Testing in C# Moq is a mocking framework built to facilitate the testing of components with dependencies. As shown earlier, dealing with dependencies could be cumbersome because it requires the creation of test doubles like fakes.


2 Answers

ASP.NET team from Microsoft have created a special version of Moq for internal usage. You can find a fork project on the asp.net github page.

How to use that?

  • Register a Moq dependencies in project.json:

    {     "frameworks": {         "dnx451": {             "dependencies": {                 "Moq": "4.2.1312.1622"             }         },         "dnxcore50": {             "dependencies": {                 "moq.netcore": "4.4.0-beta8"             }         }     } } 
  • Create a NuGet.config file in project home directory.

    <?xml version="1.0" encoding="utf-8"?>     <configuration>         <packageSources>             <add key="AspNetVNext" value="https://www.myget.org/F/aspnetcidev/api/v3/index.json" />             <add key="NuGet" value="https://api.nuget.org/v3/index.json" />         </packageSources>     </configuration> 

And use Moq! :)

like image 101
Lukasz Pyrzyk Avatar answered Sep 23 '22 10:09

Lukasz Pyrzyk


I suppose that so far a version of Moq that works with asp.net core 5.0 is not available in the nuget feed, but I think you can work with Asp.Net 5.0.

like image 35
Luca Morelli Avatar answered Sep 23 '22 10:09

Luca Morelli