Tag: mocha

断言具有相同内容的文件

我正在使用摩卡/ supertest / should.js来testing我的rest服务 GET /files/<hash>将文件作为stream返回。 如何在should.js中声明文件内容是一样的? it('should return file as stream', function (done) { var writeStream = fs.createWriteStream('test/fixtures/tmp.json'); var req = api.get('/files/676dfg1430af3595'); req.on('end', function(){ var tmpBuf = fs.readFileSync('test/fixtures/tmp.json'); var testBuf = fs.readFileSync('test/fixtures/test.json'); // How to assert with should.js file contents are the same (tmpBuf == testBuf ) // … done(); }); });

我使用毛毯在摩卡代码覆盖范围内获得0%覆盖0 SLOC

我正在尝试在MOCHA JStesting中获得代码覆盖率。 我正在使用毯子,但我得到0%的覆盖率0 SLOC为什么我不理解。 我的package.json是 { "name": "basics", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "mocha && mocha test –require blanket –reporter html-cov > coverage.html" }, "author": "", "license": "MIT", "devDependencies": { "chai": "~2.2.0", "mocha": "~2.2.4", "blanket": "~1.1.6", }, "config": { "blanket": { "pattern": ["index.js"], "data-cover-never": "node_modules" } } } 和index.js是 exports.sanitize = […]

如何使用supertest和代理在摩卡testing中进行authentication请求?

我在login后无法运行已validation的testing(服务器返回401 Unauthenticated )。 var should = require('should'), _ = require('lodash'), app = require('../../../server'), mongoose = require('mongoose'), User = mongoose.model('User'), request = require('supertest'); var user , user1; describe('GET /api/jobs', function () { before(function (done) { user = new User({ provider: 'local', name: 'Fake User', email: 'test@test.com', password: 'password' }); // Clear users before testing User.remove().exec(); request(app) […]

使用Mocha&Should.js组织testing

我是使用Mocha&should.js进行unit testing的新手。 我正在使用Mocha的BDD来testing我的应用程序。 我正在testing的应用程序有不同的组件,如帐户,产品和订单。 在将代码移动到git仓库之前,我想testing应用程序的所有方面。 我对所有组件都有不同的testing文件。 例如,账户的account.js,订单的order.js等等。 我想testing一个临时testing帐户的所有组件。 所以stream程是: 创buildtesting帐户 testing与帐户相关的所有function(更新configuration文件,更改密码等) testing帐户产品的所有function testing账户订单的所有function 删除testing帐户及与其相关的所有信息 我的问题是如何确保在执行其他testing之前创build临时帐户? 因为我在不同的文件中testing用例,所以我如何确保它们的执行顺序与上面提到的相同? 还有其他更好的方法来testing应用程序吗? 谢谢。

通过Chai发送请求

我正在试图请求我的节点JS服务器接受后/投入呼叫。 我尝试通过chai发送后调用的参数在服务器(req.body.myparam)上不可见。 我已经尝试了下面的post请求,但没有结果结束: var host = "http://localhost:3000"; var path = "/myPath"; chai.request(host).post(path).field('myparam' , 'test').end(function(error, response, body) { 和 chai.request(host).post(path).send({'myparam' : 'test'}).end(function(error, response, body) { 节点的JS代码如下: app.put ('/mypath', function(req, res){ //Handling post request to create league createDoc (req, res); }) app.post ('/mypath', function(req, res){ //Handling post request to create league createDoc (req, res); }) var createDoc […]

如何testing一个函数是否调用特定的方法/函数?

摩卡里有没有一种方法来testing一个函数是否调用特定的方法或外部函数? 我正在使用摩卡与柴,但我打开任何其他断言库。 好的,用sinon来testing一个方法是否被调用是非常容易的。 我不确定testing,看是否正在调用一个外部函数。 所以我更新了一些例子来代表一些更“真实的世界”。 我正在研究节点应用程序,所以foo.js和bar.js都是模块。 例: foo.js var bar = require('bar'); var xyz = function () {}; var Foo = module.exports = function () { this.bar(); bar(); xyz(); }; Foo.prototype.bar = function () {}; bar.js var bar = module.exports = function () {}; fooSpec.js var chai = require('chai'); var sinon = require('sinon'); var sinonChai […]

摩卡,should.js和断言exception

我有一个文件app.coffee : class TaskList class Task constructor: (@name) -> @status = 'incomplete' complete: -> if @parent? and @parent.status isnt 'completed' throw "Dependent task '#{@parent.name}' is not completed." @status = 'complete' true dependsOn: (@parent) -> @parent.child = @ @status = 'dependent' # Prepare scope stuff root = exports ? window root.TaskList = TaskList root.Task = Task […]

如何在windows node.js上运行Mochatesting(错误:找不到模块'C:\ cygdrive \ c \ Users)

我试图在Windows中运行一个应用程序,这个应用程序有一些摩卡testing。 我需要做。 我读过这个 摩卡要求make。 找不到在Windows上工作的make.exe 和这个 Node.js无法find模块 – 干扰Windows上的cygwin 我有在Github目录(cygwin目录结构之外)的应用程序,我安装了节点的Windows版本。 我试过使用PowerShell和设置别名build议,但我总是得到 > module.js:340 > throw err; > ^ Error: Cannot find module 'C:\cygdrive\c\Users\Nicola\AppData\Roaming\npm\node_modules\mocha\bin\mocha' > at Function.Module._resolveFilename (module.js:338:15) > at Function.Module._load (module.js:280:25) > at Module.runMain (module.js:487:10) > at process.startup.processNextTick.process._tickCallback (node.js:244:9) Makefile:5: recipe for target `test' failed make: *** > [test] Error 1 我在那个目录中安装了mocha(顺便说一句,为什么他不在node_modules子目录中寻找mocha?)。 问题似乎是C:\cygdrive\c\Users部分我该如何解决? 我也尝试复制文件到我的家庭/目录下的cygwin,但我得到了 ./node_modules/.bin/mocha: […]

在摩卡和SuperTest中设置基本身份validation

我试图设置一个testing,以validation用户名和密码的基本身份validation阻止的path的用户名和密码。 it('should receive a status code of 200 with login', function(done) { request(url) .get("/staging") .expect(200) .set('Authorization', 'Basic username:password') .end(function(err, res) { if (err) { throw err; } done(); }); });

在节点的mocha js中使用全局窗口variables

我是新来的jsunit testing,我正在尝试使用mocha作为我在这个github仓库find的主干联系人pipe理器教程。 然而,我有一个全局window.ContactManagervariables,我初审想testing它是否存在,然后testing启动function后面的router.onfunction。 variables看起来像这样: window.ContactManager = { Models: {}, Collections: {}, Views: {}, start: function(data) { var contacts = new ContactManager.Collections.Contacts(data.contacts), router = new ContactManager.Router(); router.on('route:home', function() { router.navigate('contacts', { trigger: true, replace: true }); }); router.on('route:showContacts', function() { var contactsView = new ContactManager.Views.Contacts({ collection: contacts }); ….. 我的testing不起作用:var expect = require('chai')。expect; describe("Application", function() { […]