僵尸可以测试一个元素不存在吗?

jnd*_*dcn 3 mocha.js node.js express zombie.js chai

我正在尝试在快速应用程序上使用zombie.js(使用mocha)以确保某些元素不会显示在页面上.这是我尝试这样做的方式:

var app = require('../app).app, // this is express but you don't care
    chai = require('chai'),
    should = chai.should(),
    Browser = require('zombie'),
    browser = new Browser();

describe("page", function() {

    it('should not have a the whatever element', function(done) {
        browser.visit('http://localhost:3000', function() {
            browser.query('#whatever').should.not.exist;
            done();
        });
    });

});
Run Code Online (Sandbox Code Playgroud)

现在,当我运行此测试时,它总是失败:

  • 如果#whatever存在,我得到这个:

    expected <div class="whatever">whatever</div> to not exist

  • 如果#whatever不存在,我希望测试通过,但我也得到一个错误

    TypeError: Cannot read property 'should' of null

也许这是一个愚蠢的测试,但有没有办法进行这样的测试,以使其通过?我哪里做错了?

谢谢.

jnd*_*dcn 7

如果其他人遇到相同的情况,我找到了解决问题的解决方案:使用chai expect而不是chai应该.

上面的代码将以这种方式转换:

var app = require('../app).app, // this is express but you don't care
    chai = require('chai'),
    expect = chai.expect,
    Browser = require('zombie'),
    browser = new Browser();

describe("page", function() {

    it('should not have a the whatever element', function(done) {
        browser.visit('http://localhost:3000', function() {
            expect(browser.query('#whatever')).not.to.exist;
            done();
        });
    });

});
Run Code Online (Sandbox Code Playgroud)

如果#whatever存在,期望断言将失败,否则它将通过.