【问题标题】:UnitTesting with Moles for tightly coupled 3rd party DLL使用 Moles 进行单元测试以实现紧密耦合的 3rd 方 DLL
【发布时间】:2012-08-09 06:22:19
【问题描述】:

我是 PEX 和 Moles 的新手,只是想知道如何使用下面的痣进行单元测试。

我只需要一个 UniDynArray 来测试。但是创建 UniDynArray 依赖于 UniSession,而 UniSession 依赖于 UniObjects.OpenConnection。当我运行此代码时,我从第三方 dll 收到错误,说会话未打开

请帮忙

[TestMethod, HostType("Moles")]
    public void Constructor2WithMoles()
    {
        using (MolesContext.Create())
        {
            //Should I make the Third party session like this ???
            MUniSession msession = new MUniSession();

            //Here What Actually Happens in the code is uniObject opensession return session
            // UniSession session = UniObjects.OpenSession(a,b,c,d); How should i do this
           //???? MUniObjects.OpenSessionStringStringStringString = (a, b, c, d) => msession;

            MUniDynArray mdata = new MUniDynArray();

            mdata.InsertInt32Int32String = (column, index, strValue) =>
                                               {
                                                   column = 1;
                                                   index = 1;
                                                   strValue = "Personal Auto";
                                               };

            mdata.InsertInt32Int32String = (column, index, strValue) =>
                                               {
                                                   column = 2;
                                                   index = 1;
                                                   strValue = "1.1";
                                               };

            mdata.InsertInt32Int32String = (column, index, strValue) =>
            {
                column = 3;
                index = 1;
                strValue = "05/05/2005";
            };

            mdata.InsertInt32Int32String = (column, index, strValue) =>
            {
                column = 4;
                index = 1;
                strValue = "Some Description";
            };

            mdata.InsertInt32Int32String = (column, index, strValue) =>
            {
                column = 5;
                index = 1;
                strValue = "20";
            };

            mdata.InsertInt32Int32String = (column, index, strValue) =>
            {
                column = 6;
                index = 1;
                strValue = "1";
            };

            History target = new History(mdata, 1);

            Assert.AreEqual<string>("Some Description", target.Description);
        }
        // TODO: CREATE Mole asserts
    }

【问题讨论】:

    标签: moles pex


    【解决方案1】:

    首先,我强烈建议在 .NET 4.5 和 Visual Studio 2012(2012 年 9 月 12 日发布)中等待使用 Fakes StubShim 类型。 Fakes 是 Moles 的产品化版本。要开始使用 Fakes,请download the free Ultimate version of VS2012 Release Candidate

    要回答您的问题,您应该使用 Dependency Injection design pattern 隔离您的第 3 方依赖项。许多 Shim/Mole 类型的用户忽略了实现依赖注入模式——仅在没有其他选项时才使用 Shim/Mole 类型。我假设第 3 方库公开了接口。 Fakes/Moles 自动将接口转换为可用于测试的 Stub 类型。您需要从接口创建具体的存根,以供生产代码使用。这些存根只是目标 3rd 方库类型的包装器。查看任何关于依赖注入的文章,了解如何实现该模式的详细信息——这种模式实现起来既快速又容易,尤其是在使用重构工具时,例如ResharperRefactor!Pro

    Stub 类型的方法调用被绕道,使用与 Shim/Mole 类型相同的 lambda/delegate 语法。存根类型适用于生成单个或少量测试所特有的存根。否则,最好的选择是创建一个用于多个测试的具体测试存根。

    【讨论】:

    • 感谢您的帮助。我使用接口和外观来包装第三方类,然后生成存根和痣。我现在可以进行测试了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-06
    • 2021-01-23
    • 1970-01-01
    • 1970-01-01
    • 2016-05-08
    • 2011-08-26
    • 1970-01-01
    相关资源
    最近更新 更多