Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
74
rated 0 times [  81] [ 7]  / answers: 1 / hits: 51074  / 11 Years ago, mon, june 17, 2013, 12:00:00

Problem



I have several tests that do the same thing in mocha. This for me, it's duplication, and is the worst thing to do when you want your system to be maintenable.



var exerciseIsPetitionActive = function (expected, dateNow) {
var actual = sut.isPetitionActive(dateNow);
chai.assert.equal(expected, actual);
};

test('test_isPetitionActive_calledWithDateUnderNumSeconds_returnTrue', function () {
exerciseIsPetitionActive(true, new Date('2013-05-21 13:11:34'));
});

test('test_isPetitionActive_calledWithDateGreaterThanNumSeconds_returnFalse', function () {
exerciseIsPetitionActive(false, new Date('2013-05-21 13:12:35'));
});


What do I need



I need a way of collapsing my duplicated mocha tests in only one.



For example, in PhpUnit (and other test frameworks) you have dataProviders.

In phpUnit a dataProvider works this way:



<?php class DataTest extends PHPUnit_Framework_TestCase {
/**
* @dataProvider provider
*/
public function testAdd($a, $b, $c)
{
$this->assertEquals($c, $a + $b);
}

public function provider()
{
return array(
array(0, 0, 0),
array(0, 1, 1),
array(1, 0, 1),
array(1, 1, 3)
);
}
}


The provider in here injects parameters to the test, and the test executes all the cases. Is perfect for duplicated test.



I want to know if in mocha is there something similar, for example, something like this:



var exerciseIsPetitionActive = function (expected, dateNow) {
var actual = sut.isPetitionActive(dateNow);
chai.assert.equal(expected, actual);
};

@usesDataProvider myDataProvider
test('test_isPetitionActive_calledWithParams_returnCorrectAnswer', function (expected, date) {
exerciseIsPetitionActive(expected, date);
});

var myDataProvider = function() {
return {
{true, new Date(..)},
{false, new Date(...)}
};
};


What I have already looked at



There is some tecnique that is called Shared Behaviours . But it does not solve the problem directly with a test suite, it just solve the problem with different components that have duplicated tests.



The Question



Do you know any way to implement dataProviders in mocha?


More From » mocha.js

 Answers
9

Mocha doesn't provide a tool for that, but it is easy to do it yourself. You only need to run the tests inside a loop and give the data to the test function using a closure:



suite(my test suite, function () {
var data = [foo, bar, buzz];
var testWithData = function (dataItem) {
return function () {
console.log(dataItem);
//Here do your test.
};
};

data.forEach(function (dataItem) {
test(data_provider test, testWithData(dataItem));
});
});

[#77591] Friday, June 14, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
yosefleod

Total Points: 113
Total Questions: 100
Total Answers: 115

Location: Egypt
Member since Tue, May 3, 2022
2 Years ago
;