错误 CS0234 类型名称空间名称'ITuple '在命名空间'System.Runtime.CompilerServices'中不存在(你是否缺少程序集引用?)[E:\DateTimeTest \DateTimeTestTests \obj \Debug \Fakes\m\f.csproj] DateTimeTestTests E:\ DateTimeTest \DateTimeTestTests\f.cs 107587 有效 错误 CS0538 $ b显式接口声明中的$ b'ITuple'不是接口[E:\DateTimeTest \DateTimeTestTests \obj \Debug \Fakes\m\f.csproj] DateTimeTestTests E:\ DateTimeTest \DateTimeTestTests\f.cs 107632 有效 如果我删除了Fakes文件夹,那么我就可以重建了。智能感知显示许多"存根"。在System.Fakes下但是没有Shims。 我试图针对不同的.Net框架,但没有发现任何有用的东西。 解决方案 嗨thomasmm123, 欢迎来到MSDN论坛。 将类库添加到单元测试参考后,请右键单击"类项目"参考,点击 添加假装大会。然后,将在单元测试项目中添加'class project.Fakes'引用和fakes文件夹。 并且,在单元测试方法中,请添加相关的命名空间。以下是一个例子: 使用System; 使用_25ClassLibrary1; 使用_25ClassLibrary1.Fakes; 使用Microsoft.QualityTools.Testing.Fakes; 使用Microsoft.VisualStudio.TestTools.UnitTesting; namespace UnitTestClass { [TestClass] public class UnitTest1 { [TestMethod] public void TestMethod1() { var now = DateTime.Now; 使用(ShimsContext.Create()) { ShimClass1.AllInstances.CurrentTimeGet = e =>现在; Class1 c1 = new Class1(); Assert.AreEqual(now,c1.CurrentTime); } } } } 请参考以下链接获取更多信息"使用填充程序将您的应用程序与其他程序集隔离,以进行单元测试" : https:// msdn.microsoft.com/en-us/library/hh549176.aspx?f=255&MSPPError=-2147217396 问候, Judyzh I am just trying to do the simple example of shimming the System.DateTime call.I am using VS Enterprise 2017 Version 15.3.2I make a Class Library(.Net Framework) project, targeting .NET Framework 4.5.2, and created the class belowusing System;namespace DateTimeTest{ public class Class1 { public DateTime CurrentTime { get; set; } public void ChangeTime() { CurrentTime = DateTime.Now; } }}I created a unit test for this class.namespace SimpleTestProject.Tests{ [TestClass()] public class Class1Tests { [TestMethod()] public void junktestTest() { Class1 c1 = new Class1(); DateTime dt = new DateTime(2017, 02, 02, 08, 52, 10); c1.ChangeTime(); Assert.AreEqual(dt, c1.CurrentTime); } }}Everything compiles fine, but as expected the test fails.In the test project I right click on the References->System and select "Add Fakes Assembly"A folder is created with mscorlid.fakes and System.fakes and under References, after about 15 seconds, System.4.0.0.0.Fakes appears.  When I attempt to rebuild the project if fails with these two errors (repeated a few time):Severity CodeDescription ProjectFile LineSuppression StateError CS0234 The type or namespace name 'ITuple' does not exist in the namespace 'System.Runtime.CompilerServices' (are you missing an assembly reference?) [E:\DateTimeTest\DateTimeTestTests\obj\Debug\Fakes\m\f.csproj]DateTimeTestTests E:\DateTimeTest\DateTimeTestTests\f.cs107587 ActiveError CS0538 'ITuple' in explicit interface declaration is not an interface [E:\DateTimeTest\DateTimeTestTests\obj\Debug\Fakes\m\f.csproj]DateTimeTestTests E:\DateTimeTest\DateTimeTestTests\f.cs107632 ActiveIf I remove the Fakes folder I am then able to rebuild. Intellisense shows many "Stubs" under System.Fakes but there are no Shims.I've tried to target different .Net Frameworks but have not found anything that works. 解决方案 Hi thomasmm123,Welcome to the MSDN forum.After you add Class Library to your unit test References, please right click ‘class project’ reference, clickAdd Fakes Assembly. Then, will add ‘class project.Fakes’ reference and a fakes folder in unit test project.And, in unit test method, please add related namespaces. Following is an example:using System;using _25ClassLibrary1;using _25ClassLibrary1.Fakes;using Microsoft.QualityTools.Testing.Fakes;using Microsoft.VisualStudio.TestTools.UnitTesting;namespace UnitTestClass{ [TestClass] public class UnitTest1 { [TestMethod] public void TestMethod1() { var now = DateTime.Now; using (ShimsContext.Create()) { ShimClass1.AllInstances.CurrentTimeGet = e => now; Class1 c1 = new Class1(); Assert.AreEqual(now, c1.CurrentTime); } } }}Please refer below links to get more information about "Using shims to isolate your application from other assemblies for unit testing": https://msdn.microsoft.com/en-us/library/hh549176.aspx?f=255&MSPPError=-2147217396Regards,Judyzh 这篇关于无法获得垫片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-18 14:11