为什么FlexUnit中没有assertError()函数?

发布于 2024-07-14 09:34:44 字数 221 浏览 7 评论 0原文

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

我知道如何实现这样的事情,并且我可能会将其添加到我的 FlexUnit 中(开源!),但这似乎是一个明显的遗漏,我想知道我是否只是做错了。

有人对此有什么想法吗?

It seems that most XUnit testing frameworks provide assertions for the times when you want to assert that a given operation will thrown an exception (or an Error in AS3 parlance.) Is there some "standard" way of doing this that I am overlooking, which would explain the absence of an assertError() assertion included with FlexUnit?

I know HOW to implement such a thing, and I will probably add it to my FlexUnit (go open source!), but it seems like such a glaring omission that I'm left wondering if I'm just doing it wrong.

Anyone have thoughts on this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

以可爱出名 2024-07-21 09:34:44

编辑 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;
}

Edit 05/02/2010: I'd now recommend using FlexUnit 4. It uses an extensible metdata system, supports expected exceptions, and also supports running in an integration server environment without the use of AIR.

Edit: You should have a look at fluint, which was built by people who had enough of FlexUnit and it's limitations. It might have some of these types of assertions built in.

I totally agree. In fact, FlexUnit is missing several useful methods (assertEvent, assertArrayEquals, etc). I know you said you know how to implement it, but feel free to use mine:

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;
}
为人所爱 2024-07-21 09:34:44

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

FlexUnit 4 mates well with hamcrest-as3. hamcrest has error assertion matchers

您的好友蓝忘机已上羡 2024-07-21 09:34:44

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

它不会取代 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();

玩得开心:)

You may want to consider using this assertion tool.

It does not replace the xxxunit framework, just facilitate the assertions you make, make them more English and less code.

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();

Have fun :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文