Mocha JS:如何在规范中重用断言?

时间:2013-08-10 22:04:51

标签: javascript mocha

我正在使用Mocha bdd进行单元测试。

在我的规范中,多个测试用例使用相同的断言。

我想将这些共享断言拉入可重用的块中。

我该怎么做?

2 个答案:

答案 0 :(得分:5)

以下是参数化测试的示例:

   'use strict';

   var assert    = require('chai').assert;
   var pascalize = require('inflection/pascalize');
   var providers = [
      { name: 'Single Word',  input: 'commons',             output: 'Commons' },
      { name: 'Single Space', input: 'creative commons',    output: 'CreativeCommons' },
      { name: 'Single Colon', input: 'creative:commons',    output: 'CreativeCommons' },
      { name: 'Double Colon', input: 'creative::commons',   output: 'CreativeCommons' },
      { name: 'Single Slash', input: 'creative/commons',    output: 'CreativeCommons' },
      { name: 'Space & Dots', input: 'creative commons...', output: 'CreativeCommons' },
   ];

   describe('pascalize', function () {

      providers.forEach(function (provider) {
         it(provider.name, function () {
            var input  = provider.input;
            var output = provider.output;

            assert(pascalize(input) === output);
         });
      });

   });

答案 1 :(得分:1)

一个普通的旧JavaScript函数怎么样?

var should = require("should");

var Vector2 = require("../assets/javascript/vector2.js").Vector2;

describe('Vector2', function(){
    var vector;

    beforeEach(function() {
        vector = new Vector2(3, -5);
    });

    // Like this vvv. See?
    function shouldThrowTypeErrorWhenNotGivenAVector2(object, func) {
        var other;

        describe('when given an object that is not a vector2', function() {
            beforeEach(function() {
                other = {};
            });

            it("should throw a TypeError", function(){
                (function() {
                    object()[func](other);
                }).should.throw(TypeError);
            });
        });
    }

    describe('#add(other)', function() {
        var other;

        it("should be defined", function (){
            vector.add.should.be.ok;
        });

        // Note that I pass vector as a function because vector is
        // being assigned in a `beforeEach` block, which isn't called
        // until you reach the inside of the `it` test case.
        shouldThrowTypeErrorWhenNotGivenAVector2(function() {return vector;}, 'add');

        describe('when given a valid vector', function() {
            beforeEach(function() {
                other = new Vector2(1, 7);
            });

            it("should return a vector with an x component equaling the sum of this and the other vector's x components", function(){
                vector.add(other).x.should.equal(4);
            });
            it("should return a vector with a y component equaling the sum of this and the other vector's y components", function(){
                vector.add(other).y.should.equal(2);
            });
        });
    });

    describe('#subtract(other)', function() {
        var other;

        it("should be defined", function (){
            vector.subtract.should.be.ok;
        });

        // Note that I pass vector as a function because vector is
        // being assigned in a `beforeEach` block, which isn't called
        // until you reach the inside of the `it` test case.
        shouldThrowTypeErrorWhenNotGivenAVector2(function() {return vector;}, 'subtract');

        describe('when given a valid vector', function() {
            beforeEach(function() {
                other = new Vector2(1, 7);
            });

            it("should return a vector with an x component equaling the difference of this and the other vector's x components", function(){
                vector.subtract(other).x.should.equal(2);
            });
            it("should return a vector with a y component equaling the difference of this and the other vector's y components", function(){
                vector.subtract(other).y.should.equal(-12);
            });
        });
    });
});

正在测试的对象:

function Vector2(x, y) {
    this.x = x;
    this.y = y;
}
Vector2.prototype.add = function(other) {
    if (!(other instanceof Vector2)) throw new TypeError("Cannot add '" + other + "'' to '" + this + "'!");
    return new Vector2(this.x + other.x, this.y + other.y);
};
Vector2.prototype.subtract = function(other) {
    if (!(other instanceof Vector2)) throw new TypeError("Cannot subtract '" + other + "'' from '" + this + "'!");
    return new Vector2(this.x - other.x, this.y - other.y);
};

// For NodeJS
if (exports === undefined) exports = {};
exports.Vector2 = Vector2;

您可以查看上述示例here的完整源代码。