Tag: sinon

Sinon js用确切的参数检查存根

Sinon js用确切的参数检查存根 要求:我想testing用正确的参数调用ejs.renderFile。 我的函数文件:html_to_pdf_converter.js var ejsToPdfConvert = function (template, data, callback) { var row = data.voucher; html = ejs.renderFile( path.join(__dirname+'/../../views/', template), { data: data }, function (error, success) { if (error) { callback(error, null); } else { var pdfPath = getPdfUploadPath(row); htmlToPdf.convertHTMLString(success, pdfPath, function (error, success) { if (error) { if (typeof callback === 'function') […]

Sinon间谍不在快速中间件内部调用

我目前正在学习testing一个jwtauthenticationexpress中间件。 我的下一个callback被称为,因为我把一个console.log在那里,但我的sinon间谍断言失败。 有人可以看看这个案子吗? 这是我的testing案例 it("should call next when the token provided is valid", () => { let token = jwt.sign({}, process.env.JWT); let request = httpMocks.createRequest({ headers: { Authorization: `Bearer ${token}` } }); const next = sinon.spy(); authenticateJwt(request, response, next); expect(next.calledOnce).to.be.true; }); 这是我的中间件 import jwt from "jsonwebtoken"; export default function(req, res, next){ const authorizationHeaders = req.headers["authorization"]; […]

意外的Sinon.js行为

我试图用Mocha和Sinon.js来存储unit testing的Stripe模块。 我需要像这样的条纹: const stripe = require('stripe'); const stubbedStripeClient = stripe.Stripe('test'); 在我的testing的根源(在我的顶级describe() )我有这样的: before('stub root', () => { sinon.stub(stripe, 'Stripe').returns(stubbedStripeClient); }); 然后,在我实际上会调用Stripe方法的describe()块中,我有这个before()钩子: let stub; before('stub', () => { console.log(typeof stubbedStripeClient.customers.create); stub = sinon.stub(stubbedStripeClient.customers, 'create', ({id: 'a-stripe-customer-id'})); }); 这是我不明白发生了什么。 钩子的第一行( console.log )输出: function 第二行引发这个exception: TypeError:尝试包装未定义的属性create as function 这怎么可能? 它怎么可能是一个线上的function,并在下一行未定义? 我看了一下Sinon.js的源码,这个检查在这里执行。 如果我然后看看他们的isFunction函数,它执行我在我的console.log相同的检查。 我感到困惑。

Sinon假XML不捕获请求

我正在尝试使用Lab和Sinon来编写一些testing,用于在我的文件中调用的各种HTTP请求。 我遵循http://sinonjs.org/上的假XMLHttpRequest示例,但是当我运行我的testing时,似乎并没有实际捕获任何请求。 这是(相关的)testing代码: context('when provided a valid payload', function () { let xhr; let requests; before(function (done) { xhr = sinon.useFakeXMLHttpRequest(); requests = []; xhr.onCreate = function (req) { requests.push(req); }; done(); }); after(function (done) { // clean up globals xhr.restore(); done(); }); it('responds with the ticket id', (done) => { create(internals.validOptions, sinon.spy()); console.log(requests); // […]

摩卡和锡诺testing与承诺超时

我有我的testing使用Sinon和摩卡以下代码。 每当我运行这些testing时,我都会看到以下的结果 0 passing (747ms) 8 pending 1 failing 1) Customer displays order Given that the order is empty "before each" hook for "will show no order items": Error: Timeout of 500ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. testing通过,直到我开始将承诺纳入图片,并使testing更现实,即设置处理asynchronous调用。 我做错了什么,如何让testing通过? tests.js 'use strict'; require("babel-register"); […]

node.js – 在mongodbunit testing中应用sinon

我使用node-mongodb-native为mongodb实现了一个模型函数: 'use strict'; const mongo = require('mongodb'); class BlacklistModel { constructor(db, tenant_id, logger) { this._db = db; this._table = 'blacklist_' + tenant_id; this._logger = logger; } create(data) { return new Promise((resolve, reject) => { const options = { unique: true, background: true, w: 1 }; this._db.collection(this._table).ensureIndex({ phone: 1 }, options, (err) => { if (err) […]

在填充调用之后模拟Mongoose对象

我试图嘲笑填充调用,我想嘲笑返回mongoose对象,而不是普通的JS对象。 sandbox.mock(BookModel) .expects('populate') .yields(null, [ new BookModel({ _id: mongoose.Types.ObjectId(), name: 'Mock Book', authorIds: [{ name: 'Foo', }] }) ]); 与模式: var BookSchema = new Schema({ name: { type: String, required: true, unique: true }, authorIds: [{type: Schema.Types.ObjectId, ref: 'Author'}], }); var AuthorSchema = new Schema({ name: { type: String, required: true, unique: true }, }); […]

节点/ Sinon – 库或模块中的存根function(node-postgres)

我试图testing一个函数,查询数据库使用PG模块,这里是我如何使用它: const { Pool } = require('pg'); const { liveDB } = require('../config/db'); const pool = new Pool(liveDB); exports.query = async (query) => { const client = await pool.connect(); try { var result = await client.query(query); console.log('result from db.query', result); return result; } catch (err) { console.log('ERROR in db.query') console.error(err); throw err; } finally { […]

使用sinon和mocha来保存pg-promise

假设我有一个以下模块,作为database.js const initOptions = {} const pgp = require('pg-promise')(initOptions) const config = require('../../config') const db = pgp({ host: config.database.host, port: config.database.port, database: config.database.database, user: config.database.user, password: config.database.password }) module.exports = db 和下面的模块一样create.js const db = require('./database') function create (name) { return new Promise((resolve, reject) => { db.func('create', name) .then(data => { return resolve(data) }) .catch(err […]

Sinon – Stub模块函数,并且不dependency injection地进行testing

我有一个代理模块,将function调用转发到服务。 我想testing是否调用服务函数,当这个代理模块中的函数被调用。 这是代理模块: const payService = require('../services/pay') const walletService = require('../services/wallet') const entity = { chargeCard: payService.payByCardToken, // … some other fn } module.exports = entity 基于这个例子和这个回应 ,我试图存根需要的模块“payService”: const expect = require('expect.js') const sinon = require('sinon') const entity = require('../entity') const payService = require('../../services/pay') describe('Payment entity,', () => { it('should proxy functions to each service', […]