Mocha and ZombieJS

倖福魔咒の 提交于 2019-11-28 04:34:13

Assuming you already have installed mocha, zombie and expect.js according to instructions, this should work for you:

// Put below in a file in your *test* folder, ie: test/sampletest.js:

var expect = require('expect.js'),
Browser = require('zombie'),
browser = new Browser();

describe('Loads pages', function(){

    it('Google.com', function(done){

        browser.visit("http://www.google.com", function () {
            expect(browser.text("title")).to.equal('Google');
            done();
        });
    });

});

Then you should be able to run the mocha command from your root application folder:

# mocha -R spec

  Loads pages
    ✓ Google.com (873ms)


  ✔ 1 tests complete (876ms)

Note: If your tests keep failing due to timeouts, it helps to increase mocha's timeout setting a bit by using the -t argument. Check out mocha's documentation for complete details.

François Zaninotto

I wrote a lengthy reply to this question explaining important gotchas about asynchronous tests, good practices ('before()', 'after()', TDD, ...), and illustrated by a real world example.

http://redotheweb.com/2013/01/15/functional-testing-for-nodejs-using-mocha-and-zombie-js.html

if you want to use cucumber-js for your acceptance tests and mocha for your "unit" tests for a page, you can use cuked-zombie (sorry for the advertising).

Install it like described in the readme on github, but place your world config in a file called world-config.js

`/* globals __dirname */
var os = require('os');
var path = require('path');

module.exports = {
  cli: null,
  domain: 'addorange-macbook': 'my-testing-domain.com',
  debug: false
};

Then use mocha with zombie in your unit tests like this:

var chai = require('chai'), expect = chai.expect;
var cukedZombie = require('cuked-zombie');

describe('Apopintments', function() {

  describe('ArrangeFormModel', function() {
    before(function(done) { // execute once
      var that = this;

      cukedZombie.infectWorld(this, require('../world-config'));

      this.world = new this.World(done);

      // this inherits the whole world api to your test
      _.merge(this, this.world);
    });

    describe("display", function() {

      before(function(done) { // executed once before all tests are run in the discribe display block
        var test = this;
        this.browser.authenticate().basic('maxmustermann', 'Ux394Ki');

        this.visitPage('/someurl', function() {
          test.helper = function() {

          };

          done();
        });
      });

      it("something on the /someurl page is returned", function() {
        expect(this.browser.html()).not.to.be.empty;
      });

If you are using Microsoft Visual Studio, you might want to take a look at Rob Ashton's Zombify. Everything is acceptably integrated so you can start writing your test cases in JavaScript or CoffeeScript. By the way, learning CoffeeScript will take you like an hour, and it's worth every minute.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!