2016-10-28 4 views
1

이 코드에서 Function.prototype.apply 메서드를 테스트하려면 어떻게해야합니까?sinon이있는 자바 스크립트 적용 테스트

var yoGen = require('yeoman-generator'); 

exports.method = function() { 

    yoBen.Base.apply(this, arguments); 

}; 

올바른 모듈에서 올바른 인수를 사용하여 해당 적용이 호출되었다고 어떻게 주장 할 수 있습니까? 나는이 같은 시도하지만 실패 : 어설의

var apply = sinon.stub(yoGen.Base,'apply'); 
testingModule.method(); 
sinon.assert.calledWith(apply,testingModule,{}); 

오류 :

apply({ ... content of testingModule ... }) != { ... content of testingModule ... } 
+0

할 일은 무엇입니까? 뭔가를 반환하는 함수입니까? – staypuftman

+0

@staypuftman 질문을 업데이트했습니다 ... 함수를 btw ... – urosjarc

답변

0

Function.prototype.apply이 기능을 통해 루프 객체를 단지 짧은 손입니다. MDN은 pretty good example to follow 보여줍니다 :

당신의 예에서
var numbers = [5, 6, 2, 3, 7]; 

// using Math.min/Math.max apply 
var max = Math.max.apply(null, numbers); 
// This about equal to Math.max(numbers[0], ...) 
// or Math.max(5, 6, ...) 

, 당신이 함수가 yoBen하지만 당신은 당신의 arguments 통해 반복 될 것입니다 무엇을 100 % 분명이 같은 수 단지 출력 결과 :

// Put apply into a variable  
var results = yoBen.Base.apply(this, arguments); 

// Just output the results and see what happened 
console.log(results); 
+0

메소드 실행 후'JSON.stringify (testingModule)'은'apply ({... content of testingModule ...})', 어떻게 도대체 내가 이것을 시험해 볼 수 있을까? – urosjarc

+0

단계로 돌아가서'console.log (apply);'결과는 무엇입니까? – staypuftman

+0

apply는 네이티브'Function.prototype.apply'입니다. 적용 출력은 '적용이 정의되지 않았습니다.'입니다. – urosjarc