【问题标题】:Why is there no assertError() function in FlexUnit?为什么 FlexUnit 中没有 assertError() 函数?
【发布时间】:2009-03-03 16:32:58
【问题描述】:

似乎大多数 XUnit 测试框架在您想要断言给定操作将引发异常(或 AS3 术语中的错误)时提供断言。是否有一些“标准”的方式来执行此操作?忽略,这可以解释没有包含在 FlexUnit 中的 assertError() 断言?

我知道如何实现这样的东西,我可能会将它添加到我的 FlexUnit(开源!),但这似乎是一个明显的遗漏,我不知道我是否只是做错了.

有人对此有想法吗?

【问题讨论】:

    标签: apache-flex actionscript-3 error-handling assertions flexunit


    【解决方案1】:

    编辑 05/02/2010:我现在建议使用 FlexUnit 4。它使用可扩展的元数据系统,支持预期异常,还支持在不使用 AIR 的情况下在集成服务器环境中运行。

    编辑:你应该看看fluint,它是由受够了 FlexUnit 的人构建的,但它的局限性。它可能内置了其中一些类型的断言。

    我完全同意。事实上,FlexUnit 缺少几个有用的方法(assertEvent、assertArrayEquals 等)。我知道你说过你知道如何实现它,但请随意使用我的:

    public static function assertError(message : String, func : Function, errorClass : Class = null, errorMessage : String = null, errorCodes : Array = null) : Error 
    {
        _assertionsMade++;
    
        if (errorClass == null) errorClass = Error;
    
        try
        {
            func();
        }
        catch(ex : Error)
        {
            if (!(ex is errorClass))
            {
                fail("Expected error of type '" + getQualifiedClassName(errorClass) + "' but was '" + getQualifiedClassName(ex) + "'");
            }
    
            if (errorMessage != null && ex.message != errorMessage)
            {
                fail("Expected error with message '" + errorMessage + "' but was '" + ex.message + "'");
            }
    
            if (errorCodes != null && errorCodes.indexOf(ex.errorID) == -1)
            {
                fail("Expected error with errorID '" + errorCodes.join(" or ") + "' but was '" + ex.errorID + "'");
            }
    
            return ex;
        }
    
        if (message == null)
        {
            message = "Expected error of type '" + getQualifiedClassName(errorClass) + "' but none was thrown"
        }
    
        fail(message);
    
        return null;
    }
    

    【讨论】:

      【解决方案2】:

      FlexUnit 4 与 hamcrest-as3 配合得很好。 hamcrest 有错误断言匹配器

      【讨论】:

        【解决方案3】:

        您可能需要考虑使用此断言工具。

        它并没有取代 xxxunit 框架,只是方便你做出断言,让它们更英文,更少代码。

        https://github.com/osher/should.as

        var p:Person = new Person();
        
        //assume a method p.sayHi()
        p.sayHi.should().throwError('name is not set');
        
        p.name = "Radagast";
        p.sayHi.should().not.throwError();
        

        玩得开心:)

        【讨论】:

          猜你喜欢
          • 2013-02-07
          • 1970-01-01
          • 1970-01-01
          • 2012-01-17
          • 2020-11-15
          • 1970-01-01
          • 2019-01-25
          • 1970-01-01
          相关资源
          最近更新 更多