无法获得垫片 [英] Can not get Shims

查看:55
本文介绍了无法获得垫片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想做一个补充System.DateTime调用的简单示例。


我正在使用VS Enterprise 2017版本15.3.2


我创建了一个针对.NET Framework 4.5.2的类库(.Net Framework)项目,并使用System创建了下面的类

; 

namespace DateTimeTest
{
public class Class1
{
public DateTime CurrentTime {get;组; }

public void ChangeTime()
{
CurrentTime = DateTime.Now;
}
}
}




我创建了这个类的单元测试。

 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);
}
}
}

所有内容编译都很好,但正如预期的那样,测试失败。


在测试项目中,我右键单击References->系统并选择"Add Fakes Assembly"


使用mscorlid.fakes和System.fakes在References下创建一个文件夹,大约15秒后,出现System.4.0.0.0.Fakes。 当我尝试重建项目时如果失败并出现这两个错误(重复几次):


严重性 代码< span style ="white-space:pre">
描述 项目
文件
抑制状态
错误 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.2

I make a Class Library(.Net Framework) project, targeting .NET Framework 4.5.2, and created the class below

using 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 Code Description Project File Line Suppression State
Error 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.cs 107587 Active


Error CS0538 'ITuple' in explicit interface declaration is not an interface [E:\DateTimeTest\DateTimeTestTests\obj\Debug\Fakes\m\f.csproj] DateTimeTestTests E:\DateTimeTest\DateTimeTestTests\f.cs 107632 Active

If 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, click Add 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=-2147217396

Regards,

Judyzh


这篇关于无法获得垫片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆