Mocha 和 ZombieJS

发布于 2025-01-07 05:39:37 字数 219 浏览 0 评论 0原文

我正在启动一个 Nodejs 项目,并希望使用 Mocha 和 Zombiejs 进行 BDD。不幸的是,我对这句话中的每一个流行词都很陌生。我可以让 Mocha 和 Zombiejs 很好地运行测试,但我似乎无法将两者集成 - 是否可以使用 Mocha 来运行 Zombiejs 测试,如果可以,那会是什么样子?

只是寻找“hello world”来帮助我开始,但教程/示例会更好。

谢谢!

I'm starting a nodejs project and would like to do BDD with Mocha and Zombiejs. Unfortunately I'm new to just about every buzzword in that sentence. I can get Mocha and Zombiejs running tests fine, but I can't seem to integrate the two - is it possible to use Mocha to run Zombiejs tests, and if so, how would that look?

Just looking for "hello world" to get me started, but a tutorial/example would be even better.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回眸一笑 2025-01-14 05:39:37

假设您已经安装了 mochazombieexpect.js 根据说明,这应该对您有用:

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

});

然后您应该能够运行来自根应用程序文件夹的 mocha 命令:

# mocha -R spec

  Loads pages
    ✓ Google.com (873ms)


  ✔ 1 tests complete (876ms)

注意:如果您的测试由于超时而持续失败,则有助于增加 mocha 的超时设置使用 -t 参数位。查看 mocha 的文档以获取完整的详细信息。

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.

慕巷 2025-01-14 05:39:37

我对这个问题写了一篇冗长的回复,解释了有关异步测试的重要问题、良好实践(“before()”、“after()”、TDD,...),并通过一个现实世界的例子进行了说明。

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

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

独留℉清风醉 2025-01-14 05:39:37

如果您想使用 cucumber-js 进行验收测试并使用 mocha 进行页面的“单元”测试,您可以使用 cuked-zombie(抱歉打广告)。

按照 github 上的自述文件中所述安装它,但将你的世界配置放在名为 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
};

然后在你的单元测试中将 mocha 与僵尸一起使用,如下所示:

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 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;
      });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文