I have created a simple C#/.Net solution with 2 projects:
- A Class Library.
- A Unit test project with a single UnitTest class, and I have added Moq to this project.
I then created three UnitTest methods, that use Moq to mock a single class, that is then injected into another class.
using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace MockSimple.Test
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
var mock = new Mock<ClassImplementingFunctionality>();
mock.Setup(functionality => functionality.Add(It.IsAny<int>(), It.IsAny<int>())).Returns(4);
var classUnderTest = new ClassUnderTest(mock.Object);
classUnderTest.Method(2,2);
mock.Verify(m => m.Add(It.IsAny<int>(), It.IsAny<int>()), Times.Exactly(1));
}
[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void TestMethod2()
{
var mock = new Mock<ClassImplementingFunctionality>();
mock.Setup(functionality => functionality.Add(It.IsAny<int>(), It.IsAny<int>())).Returns(0);
var classUnderTest = new ClassUnderTest(mock.Object);
classUnderTest.Method(10,5);
}
[TestMethod]
public void TestMethod3()
{
var ints = new List<int> {1, 2, 3, 4};
var mock = new Mock<ClassImplementingFunctionality>();
mock.Setup(functionality => functionality.Add(It.IsAny<int>(), It.IsAny<int>())).Returns(4);
var classUnderTest = new ClassUnderTest(mock.Object);
classUnderTest.LoopInts(ints);
mock.Verify(m => m.Add(It.IsAny<int>(), It.IsAny<int>()), Times.Exactly(ints.Count));
}
[TestMethod]
public void TestMethod4()
{
var ints = new List<int> { 1, 2, 3, 4, -5, -2, -7 };
var mock = new Mock<ClassImplementingFunctionality>();
mock.Setup(functionality => functionality.Add(It.IsAny<int>(), It.IsAny<int>())).Returns(4);
var classUnderTest = new ClassUnderTest(mock.Object);
classUnderTest.LoopInts(ints);
mock.Verify(m => m.Add(It.IsAny<int>(), It.IsAny<int>()), Times.Exactly(4));
}
}
}
But when trying to run the test methods using the builtin test-manager in VS2013, I get the following error in the the Test Output Window:
------ Run test started ------ The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information. Value does not fall within the expected range. Resulting in: An exception occurred while trying to create an instance of type 'Microsoft.VisualStudio.TestWindow.Model.TestGroupCollection'. Resulting in: Cannot activate part 'Microsoft.VisualStudio.TestWindow.Model.TestGroupCollection'. Element: 'Microsoft.VisualStudio.TestWindow.Model.TestGroupCollection' --> Microsoft.VisualStudio.TestWindow.Model.TestGroupCollection Resulting in: Cannot get export 'Microsoft.VisualStudio.TestWindow.Model.TestGroupCollection (ContractName="Microsoft.VisualStudio.TestWindow.Model.TestGroupCollection")' from part 'Microsoft.VisualStudio.TestWindow.Model.TestGroupCollection'. Element: Microsoft.VisualStudio.TestWindow.Model.TestGroupCollection (ContractName="Microsoft.VisualStudio.TestWindow.Model.TestGroupCollection") --> Microsoft.VisualStudio.TestWindow.Model.TestGroupCollection ========== Run test finished: 4 run (0:00:00,3464634) ==========*
I can easily run the test-methods using Resharper Unit Test Framework.
For Me it looks like MSTest is trying to do some Dependency Injection or looking for either MEF or Unity configuration.
Any Ideas?
Graci, Graci...
The command above solved the problem :-)