Pergunta

I'm trying to use zombie.js (with mocha) on an express app to make sure some elements don't show on a page. Here's how I try to do this:

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

});

Now when I run this test, it always fails:

  • if #whatever exists, I get this:

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

  • if #whatever doesn't exist, I would like the test to pass, but I also get an error

    TypeError: Cannot read property 'should' of null

Maybe this is a stupid test, but is there a way to make such a test in order to make it pass? Where am I doing wrong ?

Thx.

Foi útil?

Solução

If anyone else encounters the same situation, I have found a solution to get around the problem: use chai expect instead of chai should.

The above code would be transformed this way:

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

});

The expect assertion will fail if the #whatever exists, otherwise it will pass.

Outras dicas

A zombie.js native solution (version ^4.2.1) without need of other assertion libraries:

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

It tests whether there are exactly 0 elements matching #whatever.

documentation

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top