2 개의 프로젝트로 간단한 C # /. Net 솔루션을 만들었습니다.
그런 다음 Moq를 사용하여 단일 클래스를 모의 한 다음 다른 클래스에 주입되는 세 가지 UnitTest 메서드를 만들었습니다.
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 Unit Test Framework를 사용하여 테스트 방법을 쉽게 실행할 수 있습니다.
나를 위해 MSTest가 종속성 주입을 시도하거나 MEF 또는 Unity 구성을 찾고있는 것처럼 보입니다.
어떤 아이디어?
감사 감사 ...
개발자 명령 프롬프트에서 devenv / rootSuffix exp를 실행하십시오.
위의 명령으로 문제가 해결되었습니다. :-)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다