【问题标题】:Can zombie test that an element does not exist?僵尸可以测试一个元素不存在吗?
【发布时间】:2013-05-19 04:29:19
【问题描述】:

我正在尝试在 express 应用上使用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();
        });
    });

});

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

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

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

  • 如果 #whatever 不存在,我希望测试通过,但我也会收到错误消息

    TypeError: Cannot read property 'should' of null

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

谢谢。

【问题讨论】:

    标签: node.js express mocha.js zombie.js chai


    【解决方案1】:

    如果其他人遇到同样的情况,我已经找到了解决问题的方法:使用 chai expect 而不是 chai should。

    上面的代码会这样转换:

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

    如果 #whatever 存在,expect 断言将失败,否则它将通过。

    【讨论】:

      【解决方案2】:

      不需要其他断言库的zombie.js原生解决方案(版本^4.2.1):

      browser.assert.elements('#whatever', 0);
      

      它测试是否正好有 0 个元素匹配 #whatever

      documentation

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-01-03
        • 2013-09-24
        • 2012-03-20
        • 2010-11-09
        • 2015-09-08
        • 2011-03-14
        • 1970-01-01
        相关资源
        最近更新 更多