【发布时间】:2015-06-14 15:05:20
【问题描述】:
我创建了一个包含 2 个项目的简单 C#/.Net 解决方案:
- 一个类库。
- 具有单个 UnitTest 类的单元测试项目,我已将 Moq 添加到该项目中。
然后我创建了三个 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 配置。
有什么想法吗?
【问题讨论】:
-
这个问题是否出现在新的单元测试项目中?如果是,那么您可以尝试删除/重命名此文件夹:
%AppData%\..\Local\Microsoft\VisualStudio\12.0\ComponentModelCache并重新启动 VS。 -
是的 - 新项目也会出现该错误,并且按照说明删除缓存文件也无济于事。
-
我认为这可能是与已损坏的测试资源管理器窗口相关的设置。我仍然能够调试测试方法。
-
测试资源管理器使用 *.runsettings 文件中的设置,它不应该导致 MEF 组合错误,因为 runsettings 在初始化后被解析。您能否检查是否有导致此问题的 VS 插件?为此,请打开一个实验性 VS 实例:在开发人员命令提示符中运行
devenv /rootSuffix exp,查看问题是否在那里重现。如果它仍然重现,VS 安装修复可能会有所帮助。
标签: c# unit-testing visual-studio-2013 moq mstest