Tag: 摩卡

摩卡testing案例res.render不工作

我正在编写一个简单的路由testing用例。 我用mocha , chai-http和sinon来检查是否显示正确的页面。 这是用户提供信息的contact页面。 联系人页面现在再次呈现。 以下是我的testing案例: const chai = require('chai'); const chaiHttp = require('chai-http'); const sinon = require('sinon'); const ejs = require('ejs'); var app = require('../app'); var path = require('path'); var fs = require('fs'); var Contact = require('../models/contacts'); chai.use(chaiHttp); var should = chai.should(); var expect = chai.expect; describe('Contact', () => { describe('/POST Contact', () […]

如何testing在testing函数中没有返回的被调用的promise

首先,我一直在尝试几乎所有的东西来使这个代码工作,我取得了成功,问题是我不喜欢这种方法,我想知道是否有更好的东西,我可以做,以便使testing代码更具可读性,但function。 我想断言 (举例来说, secondApiCall )已经调用了第二个函数( secondApiCall ) ,但似乎没有办法做到这一点,你会怎么做呢。 这是否有一个非hacky方法? 这里的主要问题是“我不能修改functionToTest ”,我必须编写testing,基本上检查API调用正在完成。 如何使用给定的代码在functionToTest完成后运行断言? PS:代码是狗屎,我知道,但有时你只需要处理它,你不能再做更多的感谢只是在重构之前testing它的狗屎它:( const firstApiCall = () => { return new Promise(function(resolve,reject) { setTimeout(() => { resolve('firstApiCall success'); }, 3); }); }; const secondApiCall = () => { return new Promise(function(resolve,reject) { setTimeout(() => { resolve('secondApiCall success'); }, 3); }); }; const functionToTest = () […]

无法读取未定义的属性'then',bcrypt.hash()

我试图创build一个testing用户,在我需要散列密码的摩卡testingbefore块。 显然,如果使用promise,则不需要done()函数 。 我正在执行bcrypt.hash就像它在bcrypt文档中所说: before(function(){ //create a user and populate user's first recipes //create a User object return bcrypt.hash('newt', 10).then(function(err,hash){ //create the user object var user = new User({email:'test@test.io',username:'test',password:hash}) //save that user user.save() }) }) 但是,当我运行testing时,它给了我一个错误: TypeError: Cannot read property 'then' of undefined 是什么赋予了? 谢谢。

如何与僵尸asynchronousJavaScripttesting工作?

这个问题我创build了一个应用程序,与expressionjs和我试图用僵尸来testing它。 我已经写了一些testing来检查网页上的内容。 我已经手动检查了页面,并且正确地从我们的数据库中加载了内容。 但是,当我运行我们的testing时,我得到不可预知的结果。 这些是我们的testing。 第一个testing是失败的。 before(function(done) { models.Listing.create({name: "Studio Flat in London", text: "This is a great flat in zone one."}); models.Listing.create({name: "The Fish Market", text: "description"}); done(); }); beforeEach(function(done) { browser.visit('/', done); }); it('each property should have a title', function(done) { expect(browser.html("body")).to.include("Studio Flat in London"); }); it('each property should have a title the […]

存根内部function?

我想在unit testing时在我的代码中存储一个内部函数,例如: //foobar.js const uuid = require('uuid'); function foo() { console.log('uuid: ' + uuid.v4()); // Lots of timers } exports._foo = foo; function bar() { //Logic… foo(); //Logic… } exports.bar = bar; 而unit testing: // test/foobar.js const chai = require('chai'), expect = chai.expect, proxyquire = require('proxyquire'), sinon = require('sinon'); describe('bar', () => { it('call foo', […]

摩卡无法与Nightwatch一起运行

我正在用nighwatch.js,selenium和chromedriver运行摩卡 我可以运行与描述,但它,但我正在做一个dynamictesting,所以我需要使用下面的方式来调用,但我不能让摩卡这样的运行方式与守夜 "use strict"; var nightwatch = require('nightwatch'); var Mocha = require('mocha'); var Test = Mocha.Test; var Suite = Mocha.Suite; var mocha = new Mocha(); var suite = Suite.create(mocha.suite, 'Search Box'); suite.addTest(new Test('I\'ma dynamic test', function (done) { var client = nightwatch.initClient({ silent : true }); var browser = client.api(); browser.url(browser.launch_url); client.start(done); })); mocha.run(); 这是错误启动selenium服务器…开始 […]

在浏览器中运行seleniumbuild设者的摩卡testing

我如何在浏览器中运行testing? 我使用seleniumbuild设者来记住步骤,然后我导出file.js并运行它在摩卡(npmtesting)。 testing成功,但我不能打电话给浏览器。 如果我导出file.java并在eclipse中运行它,一切正常,但在摩卡我不能调用任何浏览器。 我已经把驱动(如geckodriver的FF)在给定的文件夹中,通过npm安装selenium服务器等,有不同的设置file.js为浏览器,命令等,但浏览器将不会出现,当我在摩卡运行testing。 (我正在使用Windows)。 我可以运行由selenium builder(.json)编写的testing。 以前在命令行启动selenium服务器; 我可以通过SeInterpreter(没有seleniumbuild设者)运行testing(.json)。 但是,我怎样才能打电话给浏览器,看我以前写过的步骤? 这是代码示例: var assert = require('assert'); var wd = require('wd'); chai = require('chai'), expect = chai.expect, _ = require('underscore'), fs = require('fs'), path = require('path'), uuid = require('uuid-js'); var VARS = {}; // This assumes that selenium is running at http://127.0.0.1:4444/wd/hub/ var noop = function() […]

摩卡testing不在根目录中运行

我在节点中有以下启动脚本: "test": "mocha **/*.test.js" 这testing了子目录中的所有文件,但不是根目录中的文件,我在这里丢失了什么?

如何debugging与Mocha运行的Eclipse中的Javascriptunit testing?

我可以用这个命令运行我的Mocha / Unit.jsunit testing: mocha fooTest.js 我想在Eclipse中设置断点并debugging这个unit testing,但是我只在Eclipse中看到以下debuggingconfiguration选项。 有没有办法在Eclipse的debugging模式下执行摩卡unit testing? 我正在使用Eclipse的JavaScript和Web开发人员氧气释放(4.7.0) 。 也许有一个插件,我可以安装或者可能创build一个node.jsdebuggingconfiguration,并使其与摩卡运行? 或者有没有办法在IntelliJ IDE中做到这一点?

共享模块无法parsing其依赖关系

我正在运行摩卡testing共享模块的问题。 问题在于,它位于其层次结构中没有node_modules的文件夹中,因此节点无法parsing模块的依赖关系。 结构如下: repo └ project_1 └ node_modules └ project_2 └ node_modules └ shared └ my_module.js 这样共享模块可以被两个项目使用。 在运行/构build项目时,共享模块将复制到项目下,以便他们可以在项目的node_modules文件夹中find它们的依赖关系。 但是,当运行mocha时,这些文件不会被复制,所以模块parsing无法find依赖关系。 对于所有项目,我可以有一个顶级的node_modules文件夹,但每个项目都有自己的package.json时可以使用这个文件夹吗? 或者,你可以给节点/摩卡path的node_modules寻找pathparsing? 有$ NODE_PATH envvariables,但它感觉不切实际,因为我将不得不改变它与项目我testing。 有这样的事情的一些最佳做法?