【问题标题】:First Moles test - redirection not happening第一次 Moles 测试 - 重定向未发生
【发布时间】:2010-07-14 12:36:13
【问题描述】:

这是代码。 GetParentPath 正常调用!

    [TestMethod]
    [HostType("Moles")]
    public void GetParentPath_slashOnEnd_returns()
    {
        var sapi = new MPhotobucketApi();
        sapi.GetParentPathString = s => s;

        var api = new PhotobucketApi();
        var parentPath = api.GetParentPath("hello/world/");

        Assert.AreEqual(parentPath, "hello");
    }

【问题讨论】:

  • 测试通过了吗?您希望发生什么不会发生的事情?
  • 对 GetParentPath 的调用应该重定向到 's => s' 委托。

标签: tdd moles


【解决方案1】:

按照您编写测试的方式,您的重定向仅适用于嵌入在 sapi 中的运行时实例。您需要拦截 PhotobucketApi 的构造函数并在其中拦截 PhotobucketApi 的“未来”实例:

MPhotobucketApi.Contructor = (me) => {
    new MPhotobucketApi { GetParentPathString = s => s }; 
};
...

另一种方法是通过这样做为所有实例重定向 GetParentPath:

MPhotobucketApi.AllInstances.GetParentPathString = (me, s) => s;
...

【讨论】:

  • 感谢您的回复 Peli。 'AllInstances' sn-p 代替了我的原始代码,但'Constructor' sn-p 没有任何效果。我更困惑的是这里第 17 页的代码...research.microsoft.com/en-us/projects/pex/molestutorial.docx。好像和我原来的代码一样。
  • Aarg 我的错,忘记将我传递给 Moles 构造函数。新的 MPhotobucketApi(me) { GetParentPathString = s => s };
猜你喜欢
  • 1970-01-01
  • 2015-02-01
  • 1970-01-01
  • 2015-09-04
  • 2020-08-13
  • 1970-01-01
  • 2022-09-27
  • 2018-11-14
  • 1970-01-01
相关资源
最近更新 更多