Per this and my own Visual Studio 2017 tinkerings:
- Go to: Tools > NuGet Package Manager > Manage NuGet Packages for Solution...
- Click the gear at the upper right.
- When the "Options" dialog box appears, click the giant green plus symbol to add an available package source.
- Add a source to: https://www.myget.org/F/aspnet-contrib/api/v3/index.json
- Close the "Options" dialog.
- Go to the "Browse" tab, click the "Include prerelease" checkbox, and select your package source from the "Package source" dropdown list.
- Search for "moq.netcore" and install it!
- Go to: Tools > NuGet Package Manager > Package Manager Console
- Run: install-package System.CodeDom YourTestProjectNameHere
- Run: install-package System.Security.Permissions YourTestProjectNameHere
- 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);
}
}
}
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!
ReplyDeleteWe 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.
DeleteThanks for reaching out! I tried created a new test solution with the template MSTest Test Project (.NET Core) and that seems to work?
DeleteI 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
Wow, good to know!
Delete