簡體   English   中英

Mocha JS:如何在規范中重用斷言?

[英]Mocha JS: How to reuse assertions within a spec?

我正在使用Mocha bdd進行單元測試。

在我的規范中,多個測試用例使用相同的斷言。

我想將這些共享斷言拉入可重用的塊中。

我怎樣才能做到這一點?

以下是參數化測試的示例:

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

   });

一個普通的舊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;

您可以在此處查看上述示例的完整源代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM