gpt4 book ai didi

javascript - 将 sinonjs 与实习生测试集成

转载 作者:行者123 更新时间:2023-11-28 07:54:53 27 4
gpt4 key购买 nike

我可以在实习生中运行测试,但我在让 spy 工作方面遇到了困难。我正在尝试整合 sinon 这样我就可以得到 spy 。这是一个示例测试文件:

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

有人知道为什么这不起作用吗?

最佳答案

正如 @Fordio 所指出的,您尝试使用的语法是 Sinon-Chai 的一部分,而不是普通的Sinon.js。您需要将该包作为依赖项,或使用 native Sinon.js assertions .

关于javascript - 将 sinonjs 与实习生测试集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26220223/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com