Tag: 摩卡

使用摩卡进行unit testing的电解质依赖性注入

在electrolyte.js中,我们通常会创build一个模块:emodule.js(example) exports=module.exports=function(module1,module2,module3){ }; exports["@singleton"]=true; exports["@require"] = ["module1","module2","module3"] 现在,如果我们想在mochatesting中真正模拟module2,那么我们应该如何调用emodule.js,这样所有对module2的调用都将被redirect到模拟? 感谢任何指针…

在摩卡testing中的for循环中进行顺序mongoose查询

我正在尝试使用摩卡作为我的API应用程序的testing驱动程序。 我有一个用户名json的歌曲名称,我试图提供给一个API端点。 JSON: { "description": "hold the lists that each user heard before", "userIds":{ "a": ["m2","m6"], "b": ["m4","m9"], "c": ["m8","m7"], "d": ["m2","m6","m7"], "e": ["m11"] } } 比方说,用户和歌曲模式只是名称的_id。 我想要做的是在parsingjson文件之后,通过按名称对“a”进行mongoose查找,将“a”转换为user_id,让我们说id = 0。 将每个“m2”转换为名称为“song_id”的id = 1,然后调用请求http:// localhost:3001 / listen / 0/1 这是我迄今为止的我的摩卡testing: test.js: it('adds the songs the users listen to', function(done){ var parsedListenJSON = require('../listen.json')["userIds"]; //console.log(parsedListenJSON); for (var […]

Node.js – 摩卡done()方法在以前的testing中导致错误

我正在实现摩卡作为我的testing框架与柴我为Node.js写一个应用程序。 这个规范是为secureId.js写的。 // secureId.js "use strict" const bcrypt = require('bcrypt'); // Constructor for SecureID function SecureID(str, rounds, func) { // Makes salt and hash unable to be changed or viewed outside the member functions let hashedID; let gennedSalt; bcrypt.genSalt(rounds, (err, salt) => { gennedSalt = salt; bcrypt.hash(str, salt, (err, hash) => { hashedID = hash; […]

我怎样才能asynchronous构build我的testing套件?

我正在尝试使用必须加载asynchronous的configuration为我的控制器创build摩卡testing。 以下是我的代码。 但是,当摩卡testing运行时,它不会运行任何testing,显示0 passing 。 console.log从未被调用。 我试过before(next => config.build().then(next))在描述内部,但即使testing运行, before也不会调用。 有没有一种方法可以在任何testing运行之前将configuration加载一次? 'use strict'; const common = require('./common'); const config = require('../config'); config .build() .then(test); function test() { console.log(1); describe('Unit Testing', () => { console.log(2); require('./auth'); }); }

在WebStorm中使用supertest会产生“参数types不能分配给参数types”和“未parsing的函数或方法”

我在WebStorm IDE中开发了一个nodejs项目。 我使用摩卡超单元作为我的unit testing框架。 WebStorm显示了两个警告: Argument type app|exports|module.exports is not assignable to parameter type Function|Server和Unresolved function or method get() 。 我尝试从File -> Settings -> Languages & Frameworks -> JavaScript -> Libraries -> Download但没有发生任何事情下载和安装supertest库。 所以我直接从https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/supertest下载它们,并手动添加它们,但WebStorm仍然产生相同的警告。 这是我的server.js代码: const express = require('express'); const app = express(); app.get('/', (req, res) => { res.status(200).end(); }); app.listen(3000); module.exports = app; 这是我的servertest.js代码: […]

摩卡单位testingmongoose模型

我正在努力弄清楚为我的NodeJS应用程序编写这些unit testing的正确(即不是黑客)的方式。 在server.js中,我将mongoose连接到本地主机上运行的数据库:27017。 当我运行我的mochatesting时,我想连接到在localhost:37017上运行的不同 mongoDB实例,以便我不针对活动数据库运行testing。 当我在test.js中需要mongoose并且尝试连接时,mongoose会抛出一个错误,说“尝试打开未closures的连接”。 我已经尝试closurestest.js中的当前连接,但由于某种原因无法正常工作。 我的问题是:什么是正确的方式来连接到一个文件中的testing数据库,但继续让server.js连接到实时数据库? 我的代码如下: // test.js var app = require('../lib/server') // This connects mongoose to a database var assert = require('assert'); var httpstatus = require('http-status'); var superagent = require('superagent'); // Connect to mongoose var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost:37017/testDB'); // THIS THROWS ERROR because server.js is connecting to localhost:27017/liveDB // Models […]

unit testing承诺不工作

即时通讯使用摩卡来testing我的API,问题是这个函数是asynchronous和testing套件之前,我从function获得resutls,我怎么能克服这个? 我尝试链接像下面的testing提出了一个错误空的testing套件。 describe("Validations", function () { var validator = require('../utils/validator'); var isValid = null; validator.validateJS() .then(function (args) { isValid = args; }).then(function(){ it("init validations ", function () { expect(isValid).to.equal('valid1'); }); }); }) 我最初的用法就像下面这个,如果是对它的调用,期望里面的答案(isValid)是来自诺言,任何想法? describe("Validations", function () { var validator = require('../utils/validator'); var isValid = null; validator.validateJS() .then(function (args) { isValid = args; }).done(); it("init validations […]

Grunt为摩卡testing提供html报告

我使用grunt来运行我的摩卡testing,我看到在控制台的testing结果是好的,问题是这个任务正在生成报告,但是当你运行这个HTML报告,你只能看到在文本中运行的日志…我希望看到testing聚合 ,和摩卡单位testing运行好,我在这里失踪? mochaTest: { test: { options: { reporter: 'spec', colors: true, summery: true, captureFile: 'results.html', // Optionally capture the reporter output to a file quiet: false, // Optionally suppress output to standard out (defaults to false) clearRequireCache: true // Optionally clear the require cache before running tests (defaults to false) }, src: ['test/*spec.js'], excludes: […]

反应酶types错误:无法读取未定义的属性'propTypes'

以下是我的React组件: import React from 'react' var PageLeftLower = React.createClass({ render:function(){ return(<a href="#">Quote Requests</a>); } }); module.exports = PageLeftLower; 所以,非常简单的React组件。 我刚开始使用Enzyme和Mocha进行testing,并编写了以下代码。 import expect from 'expect'; import React from 'react'; import {shallow} from 'enzyme'; import {PageLeftLower} from './PageLeftLower'; describe('Component : WholeTab',() => { it('renders without exploding', () => { expect(shallow(<PageLeftLower/>).length).toEqual(1); }); }); 当我执行它时,它输出以下警告: 组件:WholeTab警告:React.createElement:types不应该为null,undefined,boolean或number。 它应该是一个string(对于DOM元素)或ReactClass(对于复合组件)。 并出现以下错误: TypeError:无法读取未定义的属性“propTypes”。 […]

在POST请求中testing和存根参数

伙计们如何在POST请求中存储参数,例如这里是函数的一部分 gridCalculator : function(req,res){ // calculation logic var options=[]; options.dateFirstLicensed = req.param('DateFirstLicensed'); options.dateFirstInsured = req.param('DateFirstInsured'); options.claimList = req.param('ClaimList'); options.suspenList = req.param('SuspenList'); …etc 如果我这样做 it('grid Calculate', function (done) { var req = { 'DateFirstLicensed' : "01-01-2010", 'DateFirstInsured': "01-01-2011", 'ClaimList': ['05-03-2012'], 'SuspenList': [{'StartDate':'05-03-2012','EndDate':'05-05-2012' }] }; gridCalculator.gridCalculator(req,function (err, result) { result.should.exist; done(); }); }); 我得到错误,因为我只是传递一个对象而不是POST请求 TypeError: req.param is […]