Any suggestions for running mocha tests multiple times against different scenarios?

2,531 views
Skip to first unread message

Sam Roberts

unread,
May 15, 2013, 4:36:48 PM5/15/13
to nod...@googlegroups.com
I'm about to pull the test functions out into basically a library, and
essentially do:

describe('apples', function() {
should('taste good', fruit.tastesGood('apple'));
}

describe('oranges', function
.... tastesGood('orange')


Is there a better way? Basically I have multiple implementations of an
interface, and they should have the same behaviour, so I want them to
pass the same tests. Possibly with different beforeEach() fixtures.

Thanks,
Sam

Josh Faul

unread,
May 16, 2013, 3:14:13 PM5/16/13
to nod...@googlegroups.com
Check out the info on shared behaviors in the mocha wiki:  https://github.com/visionmedia/mocha/wiki/Shared-Behaviours

Sam Roberts

unread,
May 17, 2013, 12:43:34 AM5/17/13
to nod...@googlegroups.com
I'll check that out, thanks.

Right now I'm doing something like:

function describeFruit(type) {
describe("fruit of "+type.name, function() {
it("should be juicy", function() {
assert(type.juicy);
});});}

describeFruit("apple");

Peter Rust

unread,
May 17, 2013, 9:54:07 AM5/17/13
to nod...@googlegroups.com
Perhaps a little simpler than what you have above (though perhaps you want the additional layer, I'm not sure) is to put your tests in a loop, like:

var fruits = [{'name': 'apple', 'juicy': true}, {'name': 'orange', 'juicy': true}];
fruits.forEach(function(fruit) {
  describe(fruit.name, function() {
    it('should be juicy', function() {
      assert(fruit.juicy);
  });
});

I'll often use this pattern when I have a bunch of very similar tests -- often with arrays of inputs and expected outputs, like this:

var test_data = [
  [3, 4, 7],
  [7, 8, 15],
  [2, 1, 3]
];
describe('addition', function() {
  test_data.forEach(function(d) {
    it('should add ' + d[0] + ' + ' + d[1] + ' to equal ' + d[2], function() {
      assert.equal(d[0] + d[1], d[2]);
    });
  });
});

José F. Romaniello

unread,
May 17, 2013, 9:55:31 AM5/17/13
to nod...@googlegroups.com
+1 . I use this a lot


2013/5/17 Peter Rust <pe...@cornerstonenw.com>

--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
 
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages