Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I mock objects in the new coreclr world? [duplicate]

I have always been using Moq for mocking external dependencies while unit testing. Now that I am trying to make my code completely cross platform, I want to make my unit tests run on linux too and so I am targeting my project coreclr50.

The issue is I can't seem to find any mocking libraries that would enable me to mock on coreclr. LightMock.vNext has attempted to do it, but it requires creating mock classes manually which is not convenient.

I wonder how are people unit testing their MVC 6 controllers!

like image 717
Bilal Fazlani Avatar asked Oct 19 '22 18:10

Bilal Fazlani


2 Answers

The ASP.NET team seems to be using Moq https://github.com/aspnet/Mvc/blob/dev/test/Microsoft.AspNet.Mvc.Core.Test/project.json:

 "dnxcore50": {
  "dependencies": {
    "moq.netcore": "4.4.0-beta8"
  }
}
like image 157
Andreas Avatar answered Oct 31 '22 20:10

Andreas


NSubstitute is a great mocking framework. As far as I can see they don't directly support coreclr yet, but they have a pull request on GitHub for that:

https://github.com/nsubstitute/NSubstitute/pull/197

So you can try to do it yourself or wait a bit. Moq was a good framework, but as far as I remember it's not maintained for a long time now. NSubstitute has nicer syntax as well (at least IMHO :)

like image 43
Ilya Chernomordik Avatar answered Oct 31 '22 20:10

Ilya Chernomordik