zand3rs/sails-test-helper

语言: JavaScript

git: https://github.com/zand3rs/sails-test-helper

帆测试助手
Sails Test Helper
README.md (中文)

帆测试辅助

使用Mocha测试框架测试Sails.js的帮助程序套件。

依赖

  • 节点检验辅助
  • 抹茶
  • 否则
  • 否则钗
  • supertest
  • supertest会议
  • 帆工厂

安装

# Local install
$ npm install sails-test-helper

# Global install
$ sudo npm install -g sails-test-helper

初始化

将测试套件模板复制到当前目录。

# Local install
$ node_modules/.bin/sails-test-helper init

# Global install
$ sails-test-helper init

以下内容将复制到当前工作目录:

Makefile
test/
  factories/
  fixtures/
  helpers/
  unit/

写测试

控制器

//-- test/unit/controllers/SampleController.test.js
require("sails-test-helper");

describe(TEST_NAME, function() {
  describe("GET index", function() {
    it("should be successful", function(done) {
      request.get("/sample")
        .expect(200)
        .end(done);
    });
  });
});

执行SampleController测试

$ make test

  controllers/SampleController
    GET index
      ✓ should be successful

  1 passing

楷模

//-- test/unit/models/Sample.test.js
require("sails-test-helper");

describe(TEST_NAME, function() {
  describe(".create()", function() {
    it("should be successful", function(done) {
      Sample.create().exec(function(err, record) {
        expect(err).to.not.exist;
        expect(record).to.exist;
        done();
      });
    });
  });
});

执行Sample测试

$ make test

  models/Sample
    .create()
      ✓ should be successful 

  1 passing

测试执行

使用make命令执行测试。基本上,脚本将查找要在test / unit /目录中执行的测试。

# Run all tests
$ make test

# Run tests under a specific directory
# This will run all tests under test/unit/controllers directory
$ make test controllers

# This will run tests under test/unit/controllers and test/unit/models directories
$ make test controllers models

# Run a specific test file
# This will run tests in test/unit/controllers/SampleController.test.js file
$ make test controllers/SampleController.test

摩卡选项

Mocha选项可以作为参数传递。默认情况下,使用ff执行mocha。选项:

# recursive with 30 second timeout using spec reporter
$ mocha --recursive -t 30000 -R spec

使用MOCHA OPTS命令行变量传递特定的mocha选项。

# Dot format without colors. Useful for test execution on CI servers such as Jenkins. 
$ make MOCHA_OPTS='-C -R dot' test

助手

  • TEST_NAME
  • TEST_ROOT_PATH
  • TEST_HELPERS_PATH
  • TEST_FACTORIES_PATH
  • TEST_FIXTURES_PATH
  • requireHelper()
  • 否则
  • 存根()
  • 嘲笑()
  • 期望()
  • 应该
  • 请求
  • 作为一名囚犯

定制助手

您可以编写自己的测试助手或节点模块,并将其保存在test / helpers /目录下。使用内置的requireHelper()函数加载自定义帮助程序。

//-- test/unit/services/SampleService.test.js
require("sails-test-helper");

describe(TEST_NAME, function() {
  it("should load my custom helper", function() {
    var my_helper = requireHelper("my_helper");
    expect(my_helper).to.exist;
  });
});

如果在执行所有测试之前需要进行一些初始化,可以将它们放在test / helpers / bootstrap.js文件中。测试执行时将自动加载此文件。

//-- test/helpers/bootstrap.js
//-- global variables can also be initialized here...

before(function(done) {
  //-- anything to run or initialize before running all tests...

  done();
});

工厂

您可以使用内置工厂模块来定义,构建或创建工厂。

//-- test/unit/controllers/SampleController.test.js
require("sails-test-helper");

describe(TEST_NAME, function() {
  before(function(done) {
    //-- define a factory
    factory.define("sample")
      .attr("id", 0, {auto_increment: true});

    //-- create a record
    factory.create("sample", function(sample) {
      done();
    });
  });

  describe("GET index", function() {
    before(function(done) {
      Sample.count(function(err, count) {
        expect(err).to.not.exist;
        expect(count).to.be.greaterThan(0);
        done();
      });
    });
    it("should be successful", function(done) {
      request.get("/sample")
        .expect(200)
        .end(done);
    });
  });
});

您还可以通过本地引导文件从test / factories /目录加载工厂定义。

//-- test/helpers/bootstrap.js
before(function(done) {
  factory.load();
  done();
});

有关详细信息,请参阅sails-factory。

本文使用googletrans自动翻译,仅供参考, 原文来自github.com

en_README.md

sails-test-helper

Test helper suite for Sails.js using Mocha test framework.

Dependencies

Installation

# Local install
$ npm install sails-test-helper

# Global install
$ sudo npm install -g sails-test-helper

Initialization

Copy test suite template to current directory.

# Local install
$ node_modules/.bin/sails-test-helper init

# Global install
$ sails-test-helper init

The following will be copied to the current working directory:

Makefile
test/
  factories/
  fixtures/
  helpers/
  unit/

Writing Tests

Controllers

//-- test/unit/controllers/SampleController.test.js
require("sails-test-helper");

describe(TEST_NAME, function() {
  describe("GET index", function() {
    it("should be successful", function(done) {
      request.get("/sample")
        .expect(200)
        .end(done);
    });
  });
});

Execute SampleController test

$ make test

  controllers/SampleController
    GET index
      ✓ should be successful

  1 passing

Models

//-- test/unit/models/Sample.test.js
require("sails-test-helper");

describe(TEST_NAME, function() {
  describe(".create()", function() {
    it("should be successful", function(done) {
      Sample.create().exec(function(err, record) {
        expect(err).to.not.exist;
        expect(record).to.exist;
        done();
      });
    });
  });
});

Execute Sample test

$ make test

  models/Sample
    .create()
      ✓ should be successful 

  1 passing

Test Execution

Tests are executed using make command. Basically the script will look for tests to be executed in test/unit/ directory.

# Run all tests
$ make test

# Run tests under a specific directory
# This will run all tests under test/unit/controllers directory
$ make test controllers

# This will run tests under test/unit/controllers and test/unit/models directories
$ make test controllers models

# Run a specific test file
# This will run tests in test/unit/controllers/SampleController.test.js file
$ make test controllers/SampleController.test

Mocha Options

Mocha options can be passed as parameter to make. By default, mocha is being executed using the ff. options:

# recursive with 30 second timeout using spec reporter
$ mocha --recursive -t 30000 -R spec

Use MOCHA_OPTS commandline variable to pass specific mocha options to make.

# Dot format without colors. Useful for test execution on CI servers such as Jenkins. 
$ make MOCHA_OPTS='-C -R dot' test

Helpers

Custom Helpers

You can write your own test helpers or node modules and save it under test/helpers/ directory. Use the built-in requireHelper() function to load your custom helper.

//-- test/unit/services/SampleService.test.js
require("sails-test-helper");

describe(TEST_NAME, function() {
  it("should load my custom helper", function() {
    var my_helper = requireHelper("my_helper");
    expect(my_helper).to.exist;
  });
});

If you need to do some initialization prior to all your tests execution, you can put them inside test/helpers/bootstrap.js file. This file will be loaded automatically upon test execution.

//-- test/helpers/bootstrap.js
//-- global variables can also be initialized here...

before(function(done) {
  //-- anything to run or initialize before running all tests...

  done();
});

Factories

You can use the built-in factory module to define, build or create factories.

//-- test/unit/controllers/SampleController.test.js
require("sails-test-helper");

describe(TEST_NAME, function() {
  before(function(done) {
    //-- define a factory
    factory.define("sample")
      .attr("id", 0, {auto_increment: true});

    //-- create a record
    factory.create("sample", function(sample) {
      done();
    });
  });

  describe("GET index", function() {
    before(function(done) {
      Sample.count(function(err, count) {
        expect(err).to.not.exist;
        expect(count).to.be.greaterThan(0);
        done();
      });
    });
    it("should be successful", function(done) {
      request.get("/sample")
        .expect(200)
        .end(done);
    });
  });
});

You can also load your factory definitions from test/factories/ directory through your local bootstrap file.

//-- test/helpers/bootstrap.js
before(function(done) {
  factory.load();
  done();
});

Please see sails-factory for more details.