【问题标题】:How to pass mock object to another mock object constructor如何将模拟对象传递给另一个模拟对象构造函数
【发布时间】:2013-02-07 22:35:49
【问题描述】:
    /// <summary>
    ///A test for ReverseName
    ///</summary>
    [TestMethod()]
    public void ReverseNameTest()
    {
        Mock<IEntityName> entityName = new Mock<IEntityName>();
        entityName.SetupProperty(x => x.FirstName, "John");
        entityName.SetupProperty(x => x.LastName, "T.");

        var p = new Person(entityName.Object);

        string expected = "Your reverse name is T. John"; 
        string actual;
        actual = p.ReverseName();
        Assert.AreEqual(expected, actual);
    }
}

//人物类

  public Person(IEntityName EntityName)
    {
        this.EntityName = EntityName;
    }

是否可以在 TestMehod 中模拟 Person 类,或者我必须像上面那样创建 Person 的实例?

【问题讨论】:

    标签: c# unit-testing moq


    【解决方案1】:

    是的,你可以。 Rhino mock 支持这一点,不确定起订量,但我认为你也可以这样做

    看看这个帖子。 Passing Moq mock-objects to constructor

    Mocking objects with Moq when constructor has parameters

    【讨论】:

    • 在写这个问题之前,我确实查看了第一个链接。我正在使用 Mock.of.... 调查第二个链接。
    • 感谢这对我有用。 var p = new Mock(entityName.Object)
    猜你喜欢
    • 2018-04-29
    • 1970-01-01
    • 2017-06-02
    • 2016-11-19
    • 1970-01-01
    • 2010-09-10
    • 2017-03-07
    • 1970-01-01
    • 2011-10-24
    相关资源
    最近更新 更多