我的代码正常运行,但是我的测试用例由于其期望之一而失败。我不明白为什么我的间谍无法相信方法已经执行。
我像这样绑定事件监听器:
var playlists = Backbone.Collection.extend({
initialize: function(){
this.on('add', this._onAdd);
},
// Method:
_onAdd: function (model, collection, options) {
console.log('onAdd');
this._foo();
},
_foo: function(){
console.log('foo');
}
});
Playlists = new playlists();
Run Code Online (Sandbox Code Playgroud)
我正在用sinon监视我的对象:
it('should call _onAdd when adding a model to the collection', function() {
sinon.spy(Playlists, '_onAdd');
sinon.spy(Playlists, '_foo');
Playlists.add({});
expect(Playlists._onAdd.calledOnce).to.equal(true);
expect(Playlists._foo.calledOnce).to.equal(true);
Playlists._onAdd.restore();
Playlist._foo.restore();
});
Run Code Online (Sandbox Code Playgroud)
我的测试用例失败了,因为期望_onAdd一次被调用是不正确的。但是,_foo一次被调用的期望是正确的。
我在监视事件监听器方面做得不正确。为什么sinon不相信_onAdd被称为。我该如何纠正?
我写了一个简单的测试,我使用Sinon.js监视$ .ajax.但是,我在终端上看到"WARN [web-server]:404:/ people".它被监视时为什么要调用$ .ajax?
var people = {
findAll: function() {
return $.ajax({ url: '/people' })
}
};
var spy = sinon.spy($, 'ajax');
people.findAll();
$.ajax.restore();
Run Code Online (Sandbox Code Playgroud) 我正在尝试模拟 Node 模块中的一个函数。但它不允许我。有任何想法吗?
// module A
function foo(){
return 1;
}
function bar(){
return foo() + 1;
}
module.exports = {foo, bar}
Run Code Online (Sandbox Code Playgroud)
在测试...
const a = require('a');
...
sinon.stub(a, 'foo').callsFake(() => 3);
expect(a.bar()).to.equal(4); // gets 2 instead of 4
Run Code Online (Sandbox Code Playgroud) var stub = sinon.stub(object, "method");
Run Code Online (Sandbox Code Playgroud)
Sinon教程解释了stub,但是,如何修改上面的行以使用指定的'param'调用/ stub'meth',否则不会存根.
预期行为:我期望我的代码编译时没有错误并期望 import 语句能够工作
实际行为:当我运行 tsc app.ts 时,出现此错误:
../node_modules/@types/bluebird/index.d.ts:750:72 - error TS2583: Cannot find name 'Map'. Do you need to change your target library? Try changing the lib compiler option to es2015 or later.
750 static props<K, V>(map: Resolvable<Map<K, Resolvable>>): Bluebird<Map<K, V>>;
Run Code Online (Sandbox Code Playgroud)
游乐场链接:
我的代码编辑器中还出现另一个错误:
File '/Users/harry.gothold/Documents/api-call-weather/src/app.ts' is not a module.
Run Code Online (Sandbox Code Playgroud)
这是我正在尝试运行的测试:
import * as app from '../src/app';
// import * as chai from 'chai';
//import { result } from '../app';
var chai = require('chai');
var sinon = require('sinon');
const expect = …Run Code Online (Sandbox Code Playgroud) 流程.js
const { getConnection, closeConnection } = require('./utils/db-connection.js');
const getQueryCount = query => new Promise((resolve, rejects) => {
getConnection().then((conn) => {
conn.query(query, (err, count) => {
closeConnection(conn);
if (err) {
log.info(`Get Count Error: ${err}`);
return rejects(err);
}
return resolve(count[0][1]);
});
});
});
Run Code Online (Sandbox Code Playgroud)
流程.test.js
const dbConn = require('../src/utils/db-connection.js');
describe('all count', () => {
beforeEach(() => {
sinon.stub(dbConn, 'getConnection').resolves({ query: sinon.stub().yields(null, [[0, 5]]) });
sinon.stub(dbConn, 'closeConnection');
const process = require('./src/process'); //module is loaded after dependencies are stubbed
});
it('getQueryCount', () => { …Run Code Online (Sandbox Code Playgroud) 我正在使用此命令从NPM注册表安装模块:
npm install dc
Run Code Online (Sandbox Code Playgroud)
所有文件都已成功安装,但dc无法解析依赖项.
$ node web-test.js
module.js:340
throw err;
^
Error: Cannot find module 'sinon'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/home/nikunj/nik_verve/source/node-v0.10.20/node_modules/dc/test/env.js:25:9)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
Run Code Online (Sandbox Code Playgroud) sinon ×7
node.js ×5
javascript ×3
mocha.js ×3
chai ×2
backbone.js ×1
module ×1
npm ×1
stub ×1
typescript ×1
unit-testing ×1