问题描述:

I have a Mocha file that looks like this

 describe("when we call", function() {

function test(p) {

...

var foo = require('foo').getFoo();

chai.spy.on(foo, 'bar');

...

it("should call bar with param", function() {

expect(foo.bar).to.be.called.with(p.bat);

});

}

describe("when something", function() {

test({bat:"a"});

});

describe("when something else", function() {

test({bat:"b"});

});

});

});

This is the sequence of calls that I get

  1. describe("when something") is called
  2. describe("when something else") is called
  3. it("should call bar with param") is called for 1.
  4. it("should call bar with param") is called for 2.

The problem here is that when I inspect the calls for foo.bar in the debugger for 3. and 4. above, I see only 1 call in each case and it is the call made for 2. In other words, for both calls, the spy records that bat is "b". For this reason test 3. fails, because 3. expects the value to be "a".

What is happening here is that only the last call to the bar method is retained and the rest forgotten. How can I write this so that 3. passes with "a" and 4. passes with "b"?

相关阅读:
Top