我正在尝试使用sinon.js测试骨干应用程序.但不幸的是我因为错误而无法使用间谍方法:
TypeError: 'undefined' is not a function (evaluating 'sinon.spy()')
Run Code Online (Sandbox Code Playgroud)
以下是重现错误的步骤:
cd test && bower install sinon
<script src="bower_components/sinon/lib/sinon.js"></script>
在test/spec/test.js中创建间谍:
describe('Give it some context', function () {
describe('maybe a bit more context here', function () {
it('should run here few assertions', function () {
var spy = sinon.spy();
spy.should.be.ok;
});
});
});
Run Code Online (Sandbox Code Playgroud)用grunt运行测试: grunt test
测试将失败并显示错误.
谁能帮忙找出问题所在?
我有这样的功能:
function foo () {
obj.method(1);
obj.method(2);
obj.method(3);
}
Run Code Online (Sandbox Code Playgroud)
为了测试它,我想做3个测试(使用Mocha TDD和Sinon):
test('medthod is called with 1', function () {
var expectation = sinon.mock(obj).expects('method').once().withExactArgs(1);
foo();
expectation.verify();
});
test('medthod is called with 2', function () {
var expectation = sinon.mock(obj).expects('method').once().withExactArgs(2);
foo();
expectation.verify();
});
test('medthod is called with 3', function () {
var expectation = sinon.mock(obj).expects('method').once().withExactArgs(3);
foo();
expectation.verify();
});
Run Code Online (Sandbox Code Playgroud)
使用该系统,sinon在每次测试时都会出现"意外呼叫"消息.
我已经解决了它将树测试加入到一个:
test('medthod is called with 1, 2 and 3', function () {
var mock = sinon.mock(obj);
mock.expects('method').once().withExactArgs(1);
mock.expects('method').once().withExactArgs(2);
mock.expects('method').once().withExactArgs(3);
foo();
mock.verify();
});
Run Code Online (Sandbox Code Playgroud)
但我希望有三个测试,而不是三个断言/期望. …
当我在对象内部的函数上使用Sinon时,它可以工作:
function myFunc() {
console.log('hello');
}
var myObj = { myFunc: myFunc };
var spy = sinon.stub(myFunc);
myObj.myFunc();
expect(spy.called).to.be.true();
Run Code Online (Sandbox Code Playgroud)
但是,我不知道为什么当我在独立函数上使用Sinon时如下:
function myFunc() {
console.log('hello');
}
var spy = sinon.stub(myFunc);
myFunc();
expect(spy.called).to.be.true();
Run Code Online (Sandbox Code Playgroud)
断言失败了.
var MyClassStub = sinon.createStubInstance(MyClass);
Run Code Online (Sandbox Code Playgroud)
MyClassStub不包含静态方法.如何解决?
我开始编写一些javascript测试并试图找出检查模块构造函数的私有成员的最佳方法.例如,在下面的示例中,我使用揭示模块模式将公共API暴露给我的模块.我想测试privateVar
在$.getJSON
ajax请求的回调期间正确设置.
第二个测试it('should update privateVar', ...),
不起作用,因为myModule.privateVar
(故意)不在模块的公共API中.
所以,我的问题是,测试这种行为的最佳方法是什么,而不必将privateVar作为公共API的一部分?是否有更好的方法可以将此代码用于测试,或者使用像SinonJs这样的方法来监视私有成员?
define('myModule',
['jquery'],
function ($) {
var
myVar = "something",
privateVar = "something else",
doSomething = function() {
return $.getJSON('http://myapi.com/do-something', { requestData : "some data" }, function(response){
myVar = response.data.value1;
privateVar = response.data.value2;
});
};
return {
doSomething : doSomething,
myVar : myVar
};
}
);
define('test/test.myModule',
['myModule', 'chai', 'sinon', 'mocha'],
function (myModule, chai, sinon) {
describe("myModule", function() {
var expect = chai.expect; …
Run Code Online (Sandbox Code Playgroud) 我正在努力了解Sinon,并希望窥探console.log
.代码很简单:
function logToConsole() {
console.log('Hello World');
}
exports.logToConsole = logToConsole;
Run Code Online (Sandbox Code Playgroud)
但是,如果我想测试它,它不起作用,因为调用console.log
没有在被测系统内注册:
var chai = require('chai'),
expect = chai.expect,
sinonChai = require('sinon-chai'),
sinon = require('sinon'),
sut = require('../src/logToConsole');
chai.use(sinonChai);
describe('logToConsole', function() {
it('should spy on console.log', function() {
sinon.spy(console, 'log');
sut.logToConsole();
expect(console.log).to.have.been.called;
});
});
Run Code Online (Sandbox Code Playgroud)
但是,如果我console.log
在测试本身内部执行,则会捕获并传递:
it('should spy on console.log', function() {
sinon.spy(console, 'log');
sut.logToConsole();
console.log('Test');
expect(console.log).to.have.been.called;
});
Run Code Online (Sandbox Code Playgroud)
有趣的是,它似乎根本无法监视内部函数调用.这不是间谍图书馆的目的吗?
例如
function a() {};
function b() {
a();
}
Run Code Online (Sandbox Code Playgroud) 我为我的应用程序创建了一个数据库包装器,如下所示.为了测试它,我显然想要替换实际的数据库库.我可以创建一个新类来模拟query
方法并捕获所有输入,但使用sinon.js
似乎更合适,但我将如何使用它?
是mock
或stub
特征sinon.js
是什么,我应该使用?
wrapper = (function() {
function wrapper() {}
wrapper.db = require("database");
wrapper.prototype.insertUser = function(doc) {
return this.db.query("INSERT INTO USERS...");
};
return wrapper;
})();
Run Code Online (Sandbox Code Playgroud) 目前,我正在使用PhantomJS在我们的构建服务器上的QUnit和Sinon框架中运行Javascript单元测试.
但是,PhantomJS使用JavaScriptCore和JIT编译器作为其Javascript引擎.相反,我想使用在谷歌浏览器中使用的V8引擎,或在IE中使用的Chakra.我想这样做是因为我想检查代码的平台兼容性.
是否有像PhantomJS这样的流行测试跑步者使用这些引擎?
给出一个简单的Mongoose模型:
import mongoose, { Schema } from 'mongoose';
const PostSchema = Schema({
title: { type: String },
postDate: { type: Date, default: Date.now }
}, { timestamps: true });
const Post = mongoose.model('Post', PostSchema);
export default Post;
Run Code Online (Sandbox Code Playgroud)
我希望测试这个模型,但我遇到了一些障碍.
我当前的规范看起来像这样(为简洁起见省略了一些东西):
import mongoose from 'mongoose';
import { expect } from 'chai';
import { Post } from '../../app/models';
describe('Post', () => {
beforeEach((done) => {
mongoose.connect('mongodb://localhost/node-test');
done();
});
describe('Given a valid post', () => {
it('should create the post', (done) => {
const …
Run Code Online (Sandbox Code Playgroud) 在我的代码中,我在"确定"单击window.confirm
提示时触发回调,并且我想测试是否触发了回调.
在sinon
,我可以window.confirm
通过以下方式存根函数:
const confirmStub = sinon.stub(window, 'confirm');
confirmStub.returns(true);
Run Code Online (Sandbox Code Playgroud)
有没有办法在Jest中实现这种存根?
sinon ×10
javascript ×7
node.js ×3
mocha.js ×2
testing ×2
unit-testing ×2
backbone.js ×1
chai ×1
jestjs ×1
mongodb ×1
mongoose ×1
phantomjs ×1
qunit ×1
stub ×1