操作の一部として別のオブジェクトを作成する関数を持つオブジェクトがあるとします。
sinon = require('sinon')
chai = require 'chai'
sinonChai = require("sinon-chai")
chai.use(sinonChai)
chai.should()
Paper = {}
Paper.Origami = require('../assets/src/coffee/origami.coffee').Paper.Origami
describe '#throwOrigami', ->
it 'should create origami and throw it', ->
m = new Monkey()
throwSpy = sinon.spy(m, 'throwOrigami')
createSpy = sinon.spy(Paper, 'Origami')
# next function creates origami, then 'throws' it at someone
m.throwOrigami();
createSpy.should.have.been.calledWithNew
throwSpy.should.have.been.calledOnce
Monkey クラスには、先頭に require がありますPaper.Origami
。
テスト内で Origami を作成すればこのテストはパスしますが、Monkey オブジェクト内の作成に任せるとパスしません。これは、2 つのオブジェクト間で必要なパスが異なるためだと思われます。おそらく、ノードはそれらを同じオブジェクトとして認識していません。
質問:オブジェクト内で発生sinon
するオブジェクトの作成をスパイにスパイさせることはできますか?Origami
Monkey