将sinonjs与实习测试相结合

时间:2014-10-06 16:04:18

标签: javascript angularjs sinon intern

我可以在实习生中进行测试,但我正在努力让间谍工作。我试图整合sinon所以我可以得到间谍。这是一个示例测试文件:

define([
    'intern!bdd',
    //'intern/chai!expect',
    //'intern/order!node_modules/intern/chai',
    // 'intern/order!node_modules/chai/lib/chai',
    // 'intern/order!node_modules/sinon/lib/sinon',
    // 'intern/order!node_modules/sinon-chai/lib/sinon-chai',
    'intern/order!node_modules/sinon/lib/sinon',
    'intern/order!node_modules/sinon/lib/sinon/spy',
    'intern/order!node_modules/sinon/lib/sinon/call',
    'intern/order!node_modules/sinon/lib/sinon/behavior',
    'intern/order!node_modules/sinon/lib/sinon/stub',
    'intern/order!node_modules/sinon/lib/sinon/mock',
    'intern/order!node_modules/sinon/lib/sinon/collection',
    'intern/order!node_modules/sinon/lib/sinon/assert',
    'intern/order!node_modules/sinon/lib/sinon/sandbox',
    'intern/order!node_modules/sinon/lib/sinon/test',
    'intern/order!node_modules/sinon/lib/sinon/test_case',
    'intern/order!node_modules/sinon/lib/sinon/match',

    'intern/order!vendor/src/angular/angular',
    'intern/order!vendor/src/angular-mocks/angular-mocks',
    'intern/order!src/common/modules/error_handling/error_handling'
], function (bdd, sinon, spy, call, behavior, stub, mock, collection, assert, sandbox, test, test_case, match) {
    with (bdd) {

        sinon.spy = spy;
        sinon.call = call;
        sinon.behavior = behavior;
        sinon.stub = stub;
        sinon.mock = mock;
        sinon.collection = collection;
        sinon.assert = assert;
        sinon.sandbox = sandbox;
        sinon.test = test;
        sinon.test_case = test_case;
        sinon.match = match;

        describe('Error handler module', function () {
            var test, scope, ctrl, error_handler, log;

            function inject (fn) {
                return function() {
                    angular.injector(['ng', 'ngMock', 'error_handling']).invoke(fn);
                }
            }

            beforeEach(inject(function($log){
                log = $log;
            }));

            it('should be an object', function(){
                //expect(log).to.be.an('object');
            });

            it('should call console.trace with the string test', function(){
                var spy = sinon.spy(console.trace);

                //expect(sinon.spy).to.be.ok;

                //log.debug('test');

                console.trace('test');

                //spy.should.be.ok;

                spy.should.have.been.calledWith('test');
                //chai.expect(spy).to.have.been.called.with('test');
            });

        });
    }
});

我的基础是https://github.com/theintern/intern/blob/sinon/sinon.js

但是我错了这个错误:

>> 1/6 tests failed
Warning: FAIL: main - Error handler module - should log nothing when the logging mode is off (1ms)
TypeError: 'undefined' is not an object (evaluating 'spy.should.have')
  at </Users/evanvandegriff/Documents/work/nomi_v2/nomi_v2/web/src/common/modules/error_handling/error_handling.test.js:67>
  at <__intern/lib/Test.js:169>
  at <__intern/lib/Suite.js:237>
  at <__intern/node_modules/dojo/Deferred.js:37>
  at <__intern/node_modules/dojo/Deferred.js:258>
  at runTest  <__intern/lib/Suite.js:241>
  at <__intern/lib/Suite.js:249

任何人都有任何想法,为什么这不起作用?

1 个答案:

答案 0 :(得分:0)

正如@Fordio指出的那样,您尝试使用的语法是Sinon-Chai的一部分,而不是香草Sinon.js。您需要将该包作为依赖项,或使用本机Sinon.js assertions