19
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
   }
}

如何在上述代码示例中设置“名称”?

4

4 回答 4

15

生成的类Parent将有一个如下所示的构造函数ShimParent(Parent p)

您需要做的就是:

var child = new ShimChild();
var parent = new ShimParent(child);

并在各自的垫片上设置适当的值。

于 2014-02-22T06:33:34.023 回答
12

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

[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 以确保正确清洁您的 shim。否则,您的其他单元测试也将获得您之前声明的返回值。有关 ShimsContext 的信息可在此处找到:http: //msdn.microsoft.com/en-us/library/hh549176.aspx#ShimsContext

于 2013-03-26T08:29:18.240 回答
2

我已经根据以前的答案、Microsoft 文档和我自己的实验组合了一个解决方案。我还更改TestMethod了一点以显示我将如何实际使用它进行测试。注意:我没有编译这个特定的代码,所以如果它不能按原样工作,我深表歉意。

[TestClass]
class TestClass
{
   [TestMethod]
   public void TestMethod()
   {
      using (ShimsContext.Create())
      {
         Child child = CreateShimChild("foo", "bar");

         Assert.AreEqual("foo", child.address);  // Should be true
         Assert.AreEqual("bar", child.Name);     // Should be true
      }
   }

   private ShimChild CreateShimChild(string foo, string bar)
   {
      // Create ShimChild and make the property "address" return foo
      ShimChild child = new ShimChild() { addressGet = () => foo };

      // Here's the trick: Create a ShimParent (giving it the child)
      // and make the property "Name" return bar;
      new ShimParent(child) { NameGet = () => bar };

      return child;
   }
}

我不知道返回的孩子如何知道它Name应该返回“bar”,但确实如此!如您所见,您甚至不需要保存ShimParent任何位置;它只是为了指定Name属性的值而创建的。

于 2015-06-18T21:02:19.150 回答
0

到目前为止,我认为没有一个建议的方法可行。经过大量的试验和错误,我想出了以下对我有用的代码。基本上,您将必须定义一个初始化您的子类的委托,并在该委托中连接您的子类应继承的父 Shim。

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

        using (ShimsContext.Create())
        {
            ShimChild childShim = null;
            ShimChild.Constructor = (@this) =>
            {

                childShim = new ShimChild(@this);
                // the below code now defines a ShimParent object which will be used by the ShimChild object I am creating here
                new ShimParent()
                {
                    NameSetString = (value) =>
                    {
                        //do stuff here
                    },
                    NameGet = () =>
                    {
                        return "name";
                    }
                };
            };
        }
    }
于 2014-09-18T03:02:48.683 回答