CasperJS 1.1.0-beta2测试示例导致UncaughtError

时间:2013-11-15 23:31:18

标签: javascript testing automated-tests phantomjs casperjs

我很乐意使用CasperJS对PhantomJS无头浏览器进行测试。有很棒的文档,但我想遵循的这个例子不起作用。有没有人能够让这个工作,或者有人发现我的实施问题?我在CasperJS文档中引用了这个“Advanced Technique”:

//cow-test-ref.js
function Cow() {
    this.mowed = false;
    this.moo = function moo() {
        this.mowed = true; // mootable state: don't do that at home
        return 'moo!';
    };
}
casper.test.begin('Cow can moo', 2, {
    setUp: function(test) {
        this.cow = new Cow();
    },
    tearDown: function(test) {
        this.cow.destroy();
    },
    test: function(test) {
        test.assertEquals(this.cow.moo(), 'moo!');
        test.assert(this.cow.mowed);
        test.done();
    }
});

我的结果是以下错误:

m@mPro marketing_qa $ casperjs --version
1.1.0-beta2
m@mPro tests $ casperjs test cow-test-ref.js
Test file: cow-test-ref.js
# Cow can moo
PASS Subject equals the expected value
PASS Subject is strictly true
FAIL TypeError: 'undefined' is not a function (evaluating 'this.cow.destroy()')
#    type: uncaughtError
#    file: cow-test-ref.js:14
#    error: 'undefined' is not a function (evaluating 'this.cow.destroy()')
#           TypeError: 'undefined' is not a function (evaluating 'this.cow.destroy()')
#               at cow-test-ref.js:14
#               at done (/usr/local/Cellar/casperjs/1.1-beta2/libexec/modules/tester.js:1067)
#               at cow-test-ref.js:20
#               at /usr/local/Cellar/casperjs/1.1-beta2/libexec/modules/tester.js:986
#               at /usr/local/Cellar/casperjs/1.1-beta2/libexec/bin/bootstrap.js:51
#               at begin (/usr/local/Cellar/casperjs/1.1-beta2/libexec/modules/tester.js:1015)
#               at cow-test-ref.js:21
#    stack: not provided
FAIL 3 tests executed in 0.023s, 2 passed, 1 failed, 0 dubious, 0 skipped.

有没有人遇到过这个,或者成功地解决了这个问题?我很想使用这种测试模式。

1 个答案:

答案 0 :(得分:0)

destroy方法为例突出tearDown背后的原则概念。该方法未在原始代码中实现,因此您得到的错误是正常的。