我们可以考虑将请求发送到端点一个单元或集成测试吗? import lib from 'testing-lib'; // ... const { testClient, expect } = lib; const response = testClient .request(app) .get('/test/endpoint/'); // ... expect(response).fulfills.some.condition.ok 我感觉这是一项集成测试,因为它将确定每个作品(在请求出去的请求与响应返回)之间的每一部分都是按预期工作的.我需要知道我模糊的理解是否正确,或者我是否缺少一些细节. 解决方案 我都不会去.这是功能测试. 单元测试代码的测试单元.由此得名.代码单位通常是某种函数,类或模块. 集成测试验证我们的代码单位是否按预期共同工作.但这仍只是测试代码. 功能测试通过裸露的接口在部署状态下测试实际软件. 因此,在节点中
以下是关于 chai 的编程技术问答
我有业力下进行的单位测试,并且正在使用Sinon-Chai库. 在Mac上一切正常,但是现在我已移动到Windows以下错误: C:\Users\mchambe4\dev\simple\client>node ./node_modules/gulp/bin/gulp.js unit-tests-dev [16:29:31] Using gulpfile ~\dev\simple\client\gulpfile.js [16:29:31] Starting 'unit-tests-dev'... [16:29:31] Starting Karma server... WARN [karma]: Port 9876 in use INFO [karma]: Karma v0.12.37 server started at http://localhost:9877/ INFO [launcher]: Starting browser PhantomJS INFO [Phantom
在使用套接字建立连接时 错误:超过2000ms的超时.对于异步测试和钩子,请确保"完成( )"称为;如果返回诺言,请确保其解决. 以下是相同的代码参考 的代码参考 beforeEach(function(done) { var socketOptions = {}; var socket = io.connect("http://localhost:5000", socketOptions); socket.on('connect', function () { console.log('Connection Established'); setTimeout(done, 500); }); socket.on('error', function (err) { console.log('Connection Error', err); setTimeout
我正在编写一个单元测试来测试我的Postgres模式.我正在使用Node-PG,Mocha,Sinon和Chai. 这起作用 - 测试通过没有问题: describe('When adding a user', ()=> { it('should reject since email is used somewhere else', (done)=> { pool.query(`INSERT INTO users(email, id, token) VALUES($1, $2, $3)`, ['foo@email.com', '12346', 'fooToken']) .then((result)=> { console.log('nothing in here runs, you will not see this'); done() }) .catch((result) => {
如何测试一个自定义模块,该模块只是使用Mocha&Chai运行node-fluent-ffmpeg命令? // segment_splicer.js var config = require('./../config'); var utilities = require('./../utilities'); var ffmpeg = require('fluent-ffmpeg'); module.exports = { splice: function(raw_ad_time, crop) { if (!raw_ad_time || !crop) throw new Error("!!!!!!!!!! Missing argument"); console.log("@@@@@ LAST SEGMENT IS BEING SPLITTED."); var segment_time = utilities.ten_secon
我使用柴编写了测试.这只是三个示例测试: (实际上,还有更多测试,请查看链接) 文件:tests/2_functional-tests.js const chaiHttp = require('chai-http'); const chai = require('chai'); const assert = chai.assert; const app = require('../app'); chai.use(chaiHttp); const request = chai.request; let id1; let id2; suite('Functional Tests', function() { test("Create an issue with every field: POST request to /api/issues/{project}", async () => { const res = await request(app)
我在node.js中有一个API,可以在其中发送多个DeviceID的有效负载来更新其设置.例如,我要发送的示例有效载荷是: {"DeviceId":["1","2","3"],"Settings":[{"Key":"OnSwitch","Value":"true"}]} 发送它后,我想说的是DeviceID 1,2,3都将更新其设置.这正常工作,我已经在Postman中本地对其进行了测试.我现在想编写一个单元测试来检查行为.我的单位测试如下: context('POST With Multiple IDs', () => { describe('1,2,3 IDs POST', () => { it.only('It should post a full payload with Value True', (done) =>{ chai.request('http://localhost:3999') .post('/api/
我试图编写一个柴测试,我所做的只是流式音频并获得简单的回复:{},由于某些原因,我会遇到此错误 >流到req,如果我卸下管道,并且没有该流,则测试正常. 服务器代码: router.post('/', function (clientRequest, clientResponse) { clientRequest.on('end', function () {//when done streaming audio console.log('im at the end>>>>>'); clientResponse.setHeader('Content-Type', 'application/json'); //I've tried removing that: same result clientResponse.json({}); clientResponse.end(); //I've tried re
我希望利用Chai-HTTP进行一些测试.自然,我想测试的比我的测试更多,但是我似乎在尝试发表帖子时遇到了主要的障碍. 试图弄清楚为什么我的帖子不起作用,我开始对其进行击中测试服务器. 这是使用完全不同的工具链(茉莉节点和弗里斯比)进行测试(效果很好)的帖子尝试: frisby.create('LOGIN') .post('http://posttestserver.com/post.php', { grant_type:'password', username:'helllo@world.com', password:'password' }) .addHeader("Token", "text/plain") .expectStatus(200) }) .toss(); 导致: Time: Mon, 27 Jun 16 13:40:54 -0700 Source ip: 204.191.154.66 Heade
我正在尝试创建一个节点模块来获取一些帖子,但我遇到了一个不确定的错误. index.js var request = require('request'); function getPosts() { var options = { url: 'https://myapi.com/posts.json', headers: { 'User-Agent': 'request' } }; function callback(error, response, body) { if (!error && response.statusCode == 200) { return JSON.parse(body); } } request(options, callback); } exports.posts = getPosts; test/index.js var should =
我有可配置的中间件,可以在其中传递参数,并基于它调用下一个功能. 中间件代码: 文件:my-middleware.js exports.authUser = function (options) { return function (req, res, next) { // Implement the middleware function based on the options object next() } } var mw = require('./my-middleware.js') app.use(mw.authUser({ option1: '1', option2: '2' })) 如何使用Sinon JS模拟中间件? 我以这种方式完成了 这是我的单元测试代码: it("Should return data by id", (done: any) => { sandbox.stub(mw
我目前正在测试一个node.js/typescript应用. 我的功能应返回对象的数组. 这些对象应为类型: type myType = { title: string; description: string; level: number; categorie: string; name: string; }; 以下代码不起作用 const ach: any = await achievementsServiceFunctions.getAchievementsDeblocked(idAdmin); expect(ach) .to.be.an('array') .that.contains('myType'); 如何检查我的数组仅包含给定类型? (没有在Chai Doc上找到此信息) 解决方案 Chai没有提供直接测试其类型数组元素的直接方法.因此,假设数组的所有元素都是相同类型的,我首先测试目标确实是一个数组,然后在
我正在测试创建用户的API. API不允许创建具有相同登录值的用户.所以我写了下面的测试: const app = require('../config/express'); //exports a configured express app const request = require('supertest'); const {populateUsers} = require('../seeders/users.seed'); beforeEach(populateUsers);//drop and populate database with some seeders describe('POST /v1/users', () => { it('#Post a new user - 201 status code', (done) => { request(app) .post('/v1/users') .send({
我有一个nodejs Express应用程序,我想将其用于使用cookie的单元测试.因此,我想使用each或以前创建cookie. 无问题的代码(但没有之前的方法): import * as chai from 'chai'; import { expect } from 'chai' import chaiHttp = require('chai-http'); import { app } from '../../server'; describe('Relaties', () => { describe('Ophalen alle relaties met: GET /api/ehrm-klantnr/relatie', () => { it('should get alle relaties', (done) => { let agent = chai.request.agent(app)
这是我的先前问题 tl; dr:我正在尝试为我的nodejs全局变量声明类型(我在before钩子中设置了), 因此,打字稿可以识别它. 我的wdio.conf: ... let chai = require('chai'); let { customAssert } = require('../helpers/customAssert'); ... before: async function (capabilities, specs) { // I have accomplished to declare types for this variables thanks to the answer in the previous question global.foo = "bar" global.expect= chai.expect; global.helpers = require("../help
我正在使用摩卡咖啡 + Chai + Chai-HTTP测试我的服务器应用程序.问题是,在实际启动服务器之前,它需要做一些(主要是DB写入).这使我的测试崩溃了,因为在尚未执行服务器启动之前需要运行的任务.这是我正在使用的代码: // server declaration, it's just a restify server (async () => { await cron.scanDB(); await user.updateEventRoles(); console.log('started'); server.listen(config.port, () => { log.info('Up and running, %s listening on %s', server.name, server.url); }); })(); ... module.exports = server; 和测试: chai.request(serve
我正在进行摩卡咖啡测试.我必须在before函数中连接到mongodb,我需要在after功能中删除集合中的文档. before("authenticate user", async () => { mongoose.connect('mongodb://localhost:27017/mo-identity') db = mongoose.connection; db.once('open', function() { console.log('We are connected to test `enter code here`database!') }) .on('error', ()=>{console.error.bind(console, 'connection error')}) }) after(()=>{ db.User.
我必须使用摩卡和柴测试测试端点的响应.以下是相同的代码: async function getData (userId) { let response; let interval = setInterval(async () => { response = await superagent.get("localhost:3000/user/details/").query({'user': userId}).type('application/json'); if (response.body["status"] == 'DONE') { clearInterval(interval); response = await superagent.get("localhost:3000/user/details/get").type('appl
无论我的服务器实际返回什么,Chai总是给我res.body={}如果内容类型为"应用程序/JavaScript". 这是我的服务器: const http = require('http'); const server = http.createServer(function (request, response) { response.writeHead(200, {"Content-Type": "application/javascript"}); response.end('console.log("test");'); }); module.exports = server; server.listen(process.env.PORT || 8000); console.log("Server running at http://localhost:8000/"); 它输出console.log("test");: 但是测试看不到: