我用2个项目创建了一个简单的C#/。Net解决方案:
然后,我创建了三个UnitTest方法,这些方法使用Moq模拟单个类,然后将其注入另一个类。
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));
}
}
}
但是,当尝试使用VS2013中的内置测试管理器运行测试方法时,在“测试输出”窗口中出现以下错误:
------ 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) ==========*
我可以使用Resharper单元测试框架轻松地运行测试方法。
对我来说,似乎MSTest试图做一些依赖注入或寻找MEF或Unity配置。
有任何想法吗?
谢谢,谢谢 ...
在开发人员命令提示符中运行devenv / rootSuffix exp
上面的命令解决了问题:-)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句