Monday, April 9, 2018

I am trying to set up Moq for mocking in .NET Core!

Per this and my own Visual Studio 2017 tinkerings:

  1. Go to: Tools > NuGet Package Manager > Manage NuGet Packages for Solution...
  2. Click the gear at the upper right.
  3. When the "Options" dialog box appears, click the giant green plus symbol to add an available package source.
  4. Add a source to: https://www.myget.org/F/aspnet-contrib/api/v3/index.json
  5. Close the "Options" dialog.
  6. Go to the "Browse" tab, click the "Include prerelease" checkbox, and select your package source from the "Package source" dropdown list.
  7. Search for "moq.netcore" and install it!
  8. Go to: Tools > NuGet Package Manager > Package Manager Console
  9. Run: install-package System.CodeDom YourTestProjectNameHere
  10. Run: install-package System.Security.Permissions YourTestProjectNameHere
  11. Then what?...

 
 

I get this error:

The type initializer for 'Moq.Mock`1' threw an exception. ---> System.TypeInitializationException: The type initializer for 'Moq.Proxy.CastleProxyFactory' threw an exception. ---> System.MissingMethodException: Method not found: 'System.Security.PermissionSet System.AppDomain.get_PermissionSet()'.

 
 

When I attempt to run a simple test like so:

using System.Collections.Generic;
using GroverClevelandHopscotch.Core.Interfaces;
using GroverClevelandHopscotch.Core.Objects;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace GroverClevelandHopscotch.Core.Tests.Objects
{
   [TestClass]
   public class PresidentTests
   {
      [TestMethod]
      public void FlatFileMechanics_do_return_Presidents()
      {
         
//Arrange
         Mock<IFlatFileMechanics> presidentialMock = new Mock<IFlatFileMechanics>();
         presidentialMock.Setup(x => x.GetPresidents()).Returns(new List<President>()
         {
            new President()
            {
               Name = "Grover Cleveland",
               Party = "Democrat",
               HasNonconsecutiveTerms = true,
            },
            new President()
            {
               Name = "Benjamin Harrison",
               Party = "Republican",
               HasNonconsecutiveTerms = false,
            }
         });
         
         
//Act
         List<President> presidents = presidentialMock.Object.GetPresidents();
         
         
//Assert
         Assert.AreEqual(presidents.Count, 2);
         Assert.AreEqual(presidents[0].Name, "Grover Cleveland");
         Assert.AreEqual(presidents[0].Party, "Democrat");
         Assert.IsTrue(presidents[0].HasNonconsecutiveTerms);
         Assert.AreEqual(presidents[1].Name, "Benjamin Harrison");
         Assert.AreEqual(presidents[1].Party, "Republican");
         Assert.IsFalse(presidents[1].HasNonconsecutiveTerms);
      }
   }
}

4 comments:

  1. I have gotten this same error too. This is frustrating. This worked in .net, but core has lots of issues. Post something if you figure it out!

    ReplyDelete
    Replies
    1. We may be in a pinch where we have to wait a version for this stuff to get better. In the short term I have just been stubbing instead of mocking. https://github.com/tomjaeschke/GroverClevelandHopscotch has some examples.

      Delete
    2. Thanks for reaching out! I tried created a new test solution with the template MSTest Test Project (.NET Core) and that seems to work?

      I am able to do stuff like:
      _mockOrganizations = new Mock>();

      I think you are right about the version stuff. Looks like everyone is trying to play catch up with Microsoft and edge cases with their core stuff

      Delete