node.js - 使用 Frisbyjs 和 Nodejs 测试 POST 请求

标签 node.js rest jasmine-node frisby.js

我正在测试对/abc/getme/login 的 POST 请求,其中 header 作为内容类型,并在正文中发送一个对象。我的代码如下:

describe('HTTP Method Test for app.js', function() {
  var frisby = require('frisby');
  frisby.post('http://localhost:8000/abc/getme/login', {
    username:"myusername",
    password:"mypassword"
  }, {json: true})
  frisby.addHeaders({
    'Content-type':'application/json'

  })
  frisby.expectHeaderContains('content-type', 'application/json')
  frisby.expectJSON({
    method: 'POST'
  }).toss();
}); 

我的错误是这样的:

Failures:

  1) HTTP Method Test for app.js encountered a declaration exception
   Message:
     TypeError: frisby.addHeaders is not a function
   Stacktrace:
     TypeError: frisby.addHeaders is not a function
    at jasmine.Suite.<anonymous> (/home/srikanth/Desktop/qcs/QCS/server/spec/app_spec.js:8:20)
    at Object.<anonymous> (/home/srikanth/Desktop/qcs/QCS/server/spec/app_spec.js:2:5)
    at Module._compile (module.js:660:30)
    at Object.Module._extensions..js (module.js:671:10)
    at Module.load (module.js:573:32)
    at tryModuleLoad (module.js:513:12)
    at Function.Module._load (module.js:505:3)

最佳答案

试试这个,它是更新版本的 Frisby

var req = {
    method: "post",
    url: httpMode + "://" + url + "/v1/companies/" + companyId + "/accounts/login",
    headers : {
        "Accept": "application/json", 
        "content-type" : "application/json",
        "Authorization": "Basic " + Buffer.from(username + ":" + password).toString("base64")
    },
    body: {}
}; 

frisby.globalSetup({
    request: { 
        headers : req.headers
    }
});

it("Test login_account_spec", function(done){
    frisby.post(req.url)
        .expect("status", 200)
        .expect("header", "content-type", "application/json; charset=utf-8")
        .expect("jsonTypes", "data", {
            "user_id": Joi.string(),
            "role": Joi.string()
        })
        .then(function(res) {
            var body = res.body;
            body = JSON.parse(body);

            expect(body.data.role).toBeDefined();
            expect(body.data.user_id).toHaveLength(24);
        })
        .then(function(res) {
            record.createLogFile("login_account_spec" + dateTime, null, req, res);
        })
        .done(done);
}); 

关于node.js - 使用 Frisbyjs 和 Nodejs 测试 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48918945/

相关文章:

Spring Data REST 自定义查询集成

node.js - Jasmine Node 测试执行了两次

node.js - 未捕获的类型错误 : useValue, useFactory,数据不可迭代! Angular 4 cli项目错误

node.js - 设置 Passport-jwt 身份验证

javascript - 如何在 jwt-simple 中使 token 过期?

asp.net-mvc - 使用 ASP.NET Web API 进行跨平台身份验证

java - ActiveMQ 通过 REST 处理消息

node.js - 如何使用 jasmine-node 对非模块进行单元测试? (与关闭 Electron 中的 Node 集成有关)

node.js - 如何使用 jasmine-node 监视 Node 模块中的私有(private)函数?

javascript - Rx.js,Subscribe 被调用时未定义