标签: sinon

TypeError:'undefined'不是函数(评估'sinon.spy()')

我正在尝试使用sinon.js测试骨干应用程序.但不幸的是我因为错误而无法使用间谍方法:

TypeError: 'undefined' is not a function (evaluating 'sinon.spy()')
Run Code Online (Sandbox Code Playgroud)

以下是重现错误的步骤:

  1. 使用主干yeoman生成器创建一个空项目
  2. 安装sinon: cd test && bower install sinon
  3. 包含在test/index.html中 <script src="bower_components/sinon/lib/sinon.js"></script>
  4. 在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)
  5. 用grunt运行测试: grunt test

  6. 测试将失败并显示错误.

谁能帮忙找出问题所在?

javascript backbone.js sinon

18
推荐指数
2
解决办法
9947
查看次数

如何使用不同的参数测试对同一函数的多个调用?

我有这样的功能:

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)

但我希望有三个测试,而不是三个断言/期望. …

javascript unit-testing sinon

17
推荐指数
1
解决办法
2万
查看次数

sinon间谍独立功能

当我在对象内部的函数上使用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)

断言失败了.

sinon

17
推荐指数
2
解决办法
9846
查看次数

如何在ES6中使用sinon存根静态方法?

var MyClassStub = sinon.createStubInstance(MyClass);
Run Code Online (Sandbox Code Playgroud)

MyClassStub不包含静态方法.如何解决?

stub node.js sinon

17
推荐指数
2
解决办法
7829
查看次数

使用Sinon在Javascript中测试私有成员

我开始编写一些javascript测试并试图找出检查模块构造函数的私有成员的最佳方法.例如,在下面的示例中,我使用揭示模块模式将公共API暴露给我的模块.我想测试privateVar$.getJSONajax请求的回调期间正确设置.

第二个测试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)

javascript testing mocha.js sinon

16
推荐指数
2
解决办法
9128
查看次数

Sinon间谍在console.log上调用未注册

我正在努力了解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)

javascript testing mocha.js sinon chai

16
推荐指数
1
解决办法
1万
查看次数

在sinon.js中拼写和/或嘲弄一个班级?

我为我的应用程序创建了一个数据库包装器,如下所示.为了测试它,我显然想要替换实际的数据库库.我可以创建一个新类来模拟query方法并捕获所有输入,但使用sinon.js似乎更合适,但我将如何使用它?
mockstub特征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)

javascript node.js sinon

15
推荐指数
2
解决办法
2万
查看次数

使用V8进行Javascript单元测试

目前,我正在使用PhantomJS在我们的构建服务器上的QUnit和Sinon框架中运行Javascript单元测试.

但是,PhantomJS使用JavaScriptCore和JIT编译器作为其Javascript引擎.相反,我想使用在谷歌浏览器中使用的V8引擎,或在IE中使用的Chakra.我想这样做是因为我想检查代码的平台兼容性.

是否有像PhantomJS这样的流行测试跑步者使用这些引擎?

javascript qunit phantomjs sinon

15
推荐指数
1
解决办法
4591
查看次数

模拟/存根Mongoose模型保存方法

给出一个简单的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)

unit-testing mongoose mongodb node.js sinon

15
推荐指数
2
解决办法
1万
查看次数

Jest中的Stubbing窗口函数

在我的代码中,我在"确定"单击window.confirm提示时触发回调,并且我想测试是否触发了回调.

sinon,我可以window.confirm通过以下方式存根函数:

const confirmStub = sinon.stub(window, 'confirm');
confirmStub.returns(true);
Run Code Online (Sandbox Code Playgroud)

有没有办法在Jest中实现这种存根?

javascript sinon jestjs

15
推荐指数
3
解决办法
1万
查看次数