将数据存储在一个变量中,以便在运行frisby测试之前在REST API路由中作为参数使用
Store data in a var to use in REST API routes as params before running frisby tests
我有使用参数(电子邮件和令牌)的路由,我想在frisby测试中通过它们,但在运行测试之前,我必须在DB (mongo)中获取这些数据。
问题是我的测试运行(或不)之前,我得到的数据。
代码如下:
/* global require */
"use strict";
var frisby = require('frisby');
var emailUser;
var tokenUser;
var getDataUser = function(e) {
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://localhost/virtusBack-end', function(err, db) {
if (err) throw err;
console.log("Connected to Database");
var user = db.collection('users').findOne({
name: "virtus-app"
});
user.then(function(result) {
emailUser = result.email;
tokenUser = result.token1 + result.token2;
db.close(test(emailUser, tokenUser));
});
});
}
function test(email, token){
console.log(email);
//http://localhost:8080/rooms?email=""?token="" (IT HAS 2 PARAMS, EMAIL ADN USER TOKEN)
frisby.create('Test DB').get('http://localhost:8080/rooms')
.expectStatus(200)
.toss();
frisby.create('Test DB 2').get('http://localhost:8080/rooms')
.expectStatus(200)
.expectHeaderContains('content-type', 'application/json')
.toss();
frisby.create('Creating Room: 09')
.post('http://localhost:8080/rooms', {
_id: "000000000000000000000002",
number: 9,
floor: 0
})
.expectStatus(200)
.toss();
}
getDataUser();
函数test()应该在db关闭后运行,但我得到以下结果:
0秒完成0个测试、0个断言、0个失败、0个跳过连接数据库virtus-app.com
测试结果应该出现在console.log输出之后。我认为测试没有运行,如果它们运行,那也是在我获得数据之前。
有什么帮助吗
我们在frisbyjs中遇到了完全相同的问题,缺少像before
和after
这样的BDD钩子来收集数据并清除测试中创建的任何内容。
我最终创建了Chakram (https://github.com/dareid/chakram),它提供了BDD钩子并利用承诺,因此可以轻松编写更复杂的测试。
相关文章:
- Amazon S3 REST API大小不正确
- 客户端服务器REST API captcha实现
- 在同一ip但不同端口上使用Rest API
- 从ANGULAL连接到REST API
- 如何在使用loopback REST API时更改gulp-angular项目的端口号
- Cross domain - REST API
- 更改REST API服务器的最佳实践
- DreamFactory REST API POST休息/用户/会话请求总是在IE9中返回错误
- REST API node.js
- 数据未显示 - 余烬 js - Rest API
- 登录后无法使用 ACS REST API 发送推送通知
- 在 Rest API 中发布调用
- REST API 服务器是否应该使用 PUT 接收 JSON 数据或普通表单数据
- 如何使用已经存在的 REST API 后端在 Angular 应用程序中实现登录系统
- 获取文件并从rest api下载
- 使用Angular Resource查询具有字符串参数的REST API
- Visual Studio Team Services Rest API未授权的客户端错误
- 无法通过rest api读取json变量
- Wordpress REST API身份验证,无需登录页面
- 通过REST API/GET请求订阅Mailchimp列表