如何才能获得使用微软正版正货基类的垫片? [英] How do I get shims for base classes using Microsoft Fakes?

查看:200
本文介绍了如何才能获得使用微软正版正货基类的垫片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Parent{
   public string Name{ get; set; }
}

class Child :Parent{
   public string  address{ get; set; }
}

[TestClass]
class TestClass{
   [TestMethod]
   public void TestMethod()
   {
      var c = new Fakes.Child();
      c.addressGet = "foo"; // I can see that
      c.NameGet = "bar"; // This DOES NOT exists
   }
}

如何设置名上面的代码示例中?

How can I set the "name" in the above code sample?

推荐答案

您必须声明它的基类。最简单的方法是调用基类的AllInstances属性:

You'll have to declare it on the base class. The easiest way is to call the base class its AllInstances property:

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        ClassLibrary1.Child myChild = new ClassLibrary1.Child();

        using (ShimsContext.Create())
        {
            ClassLibrary1.Fakes.ShimChild.AllInstances.addressGet = (instance) => "foo";
            ClassLibrary1.Fakes.ShimParent.AllInstances.NameGet = (instance) => "bar";

            Assert.AreEqual("foo", myChild.address);
            Assert.AreEqual("bar", myChild.Name);
        }

    }
}



也总是尝试添加ShimsContext以确保您的垫片的正确的清洗。否则,你的其他单元测试也将获得返回的值,你以前声明。在ShimsContext
的信息可以在这里找到: http://msdn.microsoft.com/ EN-US /库/ hh549176.aspx#ShimsContext

这篇关于如何才能获得使用微软正版正货基类的垫片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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