node.js - how should I spy on a constructor that is called inside of another object? -
let's have object has function creates object part of operation.
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', -> 'should create origami , throw it', -> m = new monkey() throwspy = sinon.spy(m, 'throworigami') createspy = sinon.spy(paper, 'origami') # next function creates origami, 'throws' @ m.throworigami(); createspy.should.have.been.calledwithnew throwspy.should.have.been.calledonce
the monkey class has require @ top paper.origami
.
i can test pass if create origami within test, won't pass if leave create within of monkey object. suspect because require paths differ between 2 objects -- maybe node doesn't see them same object.
question: can sinon
spy spy on creation of origami
object happens within monkey
object?
require
resolves paths before looking in cache, should not matter paths different. however, create new paper
object origami
property in test. hence, when spy on paper, 'origami'
, property origami
of paper
object created locally in test file replaced spy. suppose next instead:
paper = require('../assets/src/coffee/origami.coffee').paper
if alter paper
object, same used in monkey
module. however, suggest using proxyquire spy or mock on dependencies.
node.js testing coffeescript sinon
No comments:
Post a Comment