Tag: sinon

sinon.stub不扼杀原始的方法

当为我的下面的代码S3resizer编写testing时,存根S3getStub在我调用testedModule时似乎不工作,我得到了来自mocha AssertionError: expected stub to have been called at least once, but it was never called的响应AssertionError: expected stub to have been called at least once, but it was never called 。 为什么S3resizer_get不被S3resizer_get ? s3Handler : 'use strict'; var s3 = new (require('aws-sdk')).S3(); var S3Handler = {}; S3Handler._get = function (bucketName, imgName, callback) { var params […]

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

var MyClassStub = sinon.createStubInstance(MyClass); MyClassStub不包含静态方法。 如何解决这个问题?

在module.exports方法中使用sinontesting方法调用

我试图testing一下,如果使用摩卡,柴和颂,给定了某些特定的条件。 这里是代码: function foo(in, opt) { if(opt) { bar(); } else { foobar(); } } function bar() {…} function foobar() {…} module.exports = { foo: foo, bar: bar, foobar:foobar }; 这是我的testing文件中的代码: var x = require('./foo'), sinon = require('sinon'), chai = require('chai'), expect = chai.expect, should = chai.should(), assert = require('assert'); describe('test 1', function () { […]

用sinon剔除条纹 – 使用stub.yields

我试图用sinon存根nodejs stripe api来testing客户的创build,使用如下的testing: var sinon = require('sinon'); var stripe = require('stripe'); var controller = require('../my-controller'); var stub = sinon.stub(stripe.customers, 'create'); stub.create.yields([null, {id: 'xyz789'}]); //stub.create.yields(null, {id: 'xyz789'}); //same result with or without array controller.post(req, {}, done); 我的理解是, stub.create.yields应该调用第一个callback,并传递它(在这种情况下)null,后面跟着一个ID为xyz789的对象。 这可能是我误解的地方 在我的'控制器'我有以下内容: exports.post = function(req, res, next) { stripe.customers.create({ card: req.body.stripeToken, plan: 'standard1month', email: req.body.email }, function(err, customer) […]

模拟fs.readdir进行testing

我试图嘲笑函数fs.readdir为我的testing。 起初我试过使用sinon,因为这是一个非常好的框架,但是没有奏效。 stub(fs, 'readdir').yieldsTo('callback', { error: null, files: ['index.md', 'page1.md', 'page2.md'] }); 我的第二个尝试是用自我replace函数来模拟函数。 但它也不起作用。 beforeEach(function () { original = fs.readdir; fs.readdir = function (path, callback) { callback(null, ['/content/index.md', '/content/page1.md', '/content/page2.md']); }; }); afterEach(function () { fs.readdir = original; }); 有谁可以告诉我为什么两个都不行? 谢谢! 更新 – 这也不起作用: sandbox.stub(fs, 'readdir', function (path, callback) { callback(null, ['index.md', 'page1.md', 'page2.md']); }); […]

存根/模拟process.platform sinon

我正在使用process.platform并希望存根string值来伪造不同的操作系统。 (这个对象是生成在我的范围之外,我需要testing它可以采取不同的值) 是否有可能残留/伪造这个值? 我已经尝试了以下没有任何运气: stub = sinon.stub(process, "platform").returns("something") 我得到错误TypeError: Attempted to wrap string property platform as function 同样的事情发生,如果我尝试使用这样的模拟: mock = sinon.mock(process); mock.expects("platform").returns("something");

如何unit testing一个函数调用另一个返回一个承诺?

我有一个使用快递4的node.js应用程序,这是我的控制器: var service = require('./category.service'); module.exports = { findAll: (request, response) => { service.findAll().then((categories) => { response.status(200).send(categories); }, (error) => { response.status(error.statusCode || 500).json(error); }); } }; 它呼吁我的服务返回一个承诺。 一切正常,但我试图unit testing时遇到了麻烦。 基本上,我想确保根据我的服务返回,我用正确的状态代码和正文刷新响应。 所以摩卡和sinon看起来像这样: it('Should call service to find all the categories', (done) => { // Arrange var expectedCategories = ['foo', 'bar']; var findAllStub = sandbox.stub(service, 'findAll'); […]

节点jsunit testing:嘲笑需要依赖

我正在为以下设置编写unit testing的问题作为jira.js文件(在node.js模块中): var rest = require('restler'); // https://www.npmjs.com/package/restler module.exports = function (conf) { var exported = {}; exported.getIssue = function (issueId, done) { … rest.get(uri).on('complete', function(data, response) { … }; return exported; }; 现在,我想为我的getIssue函数编写unit testing。 'restler'是一个REST客户端,我通过REST客户端调用JIRA API来获取JIRA问题。 所以为了能够testingcreateIssue(..),我希望能够在我的Jasmineunit testing中嘲笑'rest'var。 我怎样才能嘲笑这种方法? 请给我一些指示,以便我可以继续。 我曾尝试使用rewire,但我失败了。 这是我迄今为止不起作用(即getIssue方法原来是未定义的): var rewire = require("rewire"); var EventEmitter = require('events').EventEmitter; var emitter = new […]

嘲笑在同一模块内导出和调用的模块function?

新的unit testing和间谍,存根和嘲笑的概念。 我想从下面的代码testingpassword.js的verify方法,但是我无法在testing文件中存储hash函数。 由于verify使用hash函数和hash函数导出,我虽然我应该stub hash函数返回一个固定的响应,而不是实际调用hash 。 因为我不想testinghash函数。 问题:testingverify时不会调用创build的hash函数存根。 问题1:我应该把重点放在testing函数本身的逻辑上而不是另一个函数上吗? 主要问题:(已 回答)如何在同一个模块中调用模块函数? 方面的问题2:如果我不知道哪里没有导出,而只停留在模块中,我将如何去做哈希哈希? 码 password.js /** * Creates a hash based on a salt from a given password * if there is no salt a new salt will be generated * * @param {String} password * @param {String} [salt] Optional value, if not given will generate […]

如何多次声明stubbed的提取

使用proxyquire,sinon和mocha。 我可以在第一次调用fetch时存根。 但是在recursion调用的第二次调用中,我不能断言它。 从输出结果看,在testing结束之前,断言可能会运行。 在断言之后,您将会看到second fetch控制台。 index.js var fetch = require('node-fetch'); function a() { console.log('function a runs'); fetch('https://www.google.com') .then((e) => { console.log('first fetch'); b(); }) .catch((e)=> { console.log('error') }); } function b() { fetch('https://www.google.com') .then((e) => { console.log('second fetch'); }) .catch((e)=> { console.log('error') }); } a() testing: describe('fetch test demo', ()=> { it('fetch should of […]