问题
I'm using Mocha bdd for unit testing.
Within my specifications, multiple test cases make use of the same assertions.
I would like to pull these shared assertions out into a reusable block.
How can I do this?
回答1:
Here is an example of parameterized tests:
'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);
});
});
});
回答2:
How about a plain old JavaScript function?
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);
});
});
});
});
Object under test:
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;
You can view the full source code for the above example here.
来源:https://stackoverflow.com/questions/18166770/mocha-js-how-to-reuse-assertions-within-a-spec