Tag: 摩卡

TypeError:无法读取未定义超类的属性“地址”

我需要一些帮助来解决testingnodejs代码的问题。 我正在使用摩卡和超级特技。 我很困惑与supertest的实施。 我不知道要解决这个问题。 我试图自动下载一个文件。 `describe('GET /entry/:entryId/file/:id/download', function(){ it('should pass download function', function(done){ this.timeout(15000); request(app.webServer) .get('/entry/543CGsdadtrE/file/wDRDasdDASAS/download') .set('Authorization', 'Bearer eyJ0eXAiOiJKV1QiLCJhbGco') .expect(200) .end(function(err, res){ if (err) return done(err); console.log(err, res); done(); }); }); });

用嘲讽和sinon嘲笑一个类的方法

我正在学习使用sinon的节点模块嘲讽进行unit testing。 只使用嘲笑和普通的类,我能够成功地注入一个模拟。 不过我想注入一个sinon stub而不是一个普通的类,但是我遇到了很多麻烦。 我试图嘲笑的课程: function LdapAuth(options) {} // The function that I want to mock. LdapAuth.prototype.authenticate = function (username, password, callback) {} 以下是我在beforeEach()函数中使用的代码: beforeEach(function() { ldapAuthMock = sinon.stub(LdapAuth.prototype, "authenticate", function(username, password, callback) {}); mockery.registerMock('ldapauth-fork', ldapAuthMock); mockery.enable(); }); afterEach(function () { ldapAuthMock.restore(); mockery.disable(); }); 我试图以各种方式来模拟/存根LdapAuth类而没有成功,上面的代码只是最新的版本,不起作用。 所以我只想知道如何用颂歌和嘲弄来成功地嘲笑这个。

在使用Sinon时,如何replace存根实例中的存根函数?

如果我通过var a = sinon.createStubInstance(MyContructor)创build一个实例。 我怎样才能取代一个var stub = sinon.stub(object, "method", func);函数像var stub = sinon.stub(object, "method", func); 。 我这样做的主要原因是要实现多个callback的解决方法, 如上所述

Sails.js:如何实际运行testing

我对帆,节点和js是全新的,所以我可能会漏掉一些明显的东西。 我正在使用风帆0.10.5和节点0.10.33。 在sails.js文档中有一个关于testinghttp://sailsjs.org/#/documentation/concepts/Testing的页面,但是它并没有告诉我如何真正运行它们。 我已经根据该文档设置了目录,添加了一个名为test / unit / controllers / RoomController.test.js的testing,现在我希望它运行。 没有“帆testing”命令或类似的东西。 我也没有find有关如何添加任务的任何迹象,所以testing总是在“帆升”之前运行。

保持摩卡testing与源文件一起

我目前在src和testing套件中有我的NodeJS源文件,例如: /src/bar/baz/foo.js /test/bar/baz/foo.spec.js 这导致了像var foo = require('../../../src/bar/baz/foo')这样的尴尬需求语句。 一目了然,很难一眼看出哪些源文件缺lesstesting。 我想将我的testing套件保存在与相关源文件相同的目录中: /src/bar/baz/foo.js /src/bar/baz/foo.spec.js 但是现在运行mocha –recursive src会导致错误,因为摩卡试图运行我的源文件作为testing。 我见过使用find或gulp来过滤文件列表的build议,但是我发现使用普通的Mocha无法做到这一点令人惊讶。 这种组织文件的方式是什么?

不能在sailsjs中unit testing我的模型

对于我的帆应用程序,我使用下面的代码来unit testing用户模型,但得到了错误信息: 'TypeError:对象#没有方法'创build'' var User = require('../../api/models/User'); var Sails = require('sails'); console.log(User); describe("User Model:", function() { // create a variable to hold the instantiated sails server var app; // Global before hook before(function(done) { // Lift Sails and start the server Sails.lift({ log: { level: 'error' }, }, function(err, sails) { app = sails; done(err, […]

节点摩卡数组应该包含一个元素

我想做一个简单的断言 knownArray.should.include('known value') 该数组是正确的,但我根本无法找出正确的断言用来检查数组是否有这个值(索引不重要)。 我也尝试了should.contain但是这两个都抛出一个错误,即Object #<Assertion> has no method 'contain' should.contain Object #<Assertion> has no method 'contain' (或'include' ) 我怎样才能检查一个数组包含使用should的元素?

mongoose模型testing需要模型

我有一个testing我的mongoose模型的问题 我有一个像 应用 楷模 地址 用户 组织 testing 用户和组织都需要知道型号Address。 我的模型结构如下: module.exports = function (mongoose, config) { var organizationSchema = new mongoose.Schema({ name : { type : String }, addresses : { type : [mongoose.model('Address')] } }); var Organization = mongoose.model('Organization', organizationSchema); return Organization; }; 在我的正常的应用程序,我需要地址之前要求用户和组织,一切都很好。 我现在为用户和组织编写testing。 为了让地址模型注册,我打电话require('../models/Address.js')这工作正常,如果我运行一个testing。 但是,如果我在批处理中运行所有testing,我得到一个错误,因为我试图注册地址两次。 OverwriteModelError: Cannot overwrite Address model once compiled. […]

如何通过Passport / Facebook战略authenticationSupertest请求/?

我使用Passport.js进行身份validation(Facebook策略),并使用Mocha和Supertest进行testing。 我如何创build一个会话,并与Supertest Facebook的策略进行authentication的请求? 以下是用户未login时的示例testing: describe 'when user not logged in', -> describe 'POST /api/posts', -> it 'respond with 401', (done)-> request(app). post(API.url('posts')). set('Accept', 'application/json'). send(post: data). expect('Content-Type', /json/). expect(401, done) 谢谢你的build议:D

在sinon中断言recursion调用的次数

我有以下队列消费者类通过承诺recursion运行: "use strict"; var queue = require("./queue"), helpers = require("./helpers"), vendors = require("../config/vendors"), queueConf = require("../config/queue"); function Consumer() { this.queue = new queue.TaskQueue(); this.currentItem = null; this.port = null; this.payload = null; } Consumer.prototype.payloadSuccessCb = function (data) { this.payload = data; this.run(); }; Consumer.prototype.failureCb = function (data) { console.error(data); throw new Error(data); //TODO: Continue queue […]