简体   繁体   English

如何在Frisby Test中验证状态码是200还是201

[英]How to verify if either 200 or 201 status code in Frisby Test

I'm using frisby v0.8.5 and jasimine-node . 我正在使用frisby v0.8.5和jasimine-node With frisby there's the following to assert for expected return http status code: 使用frisby可以声明以下内容以返回预期的HTTP状态代码:

 f = frisby.create(/*stuff....*/);
 ...
 ... 
 f.expectStatus(200);
 f.toss();

This works fine if the endpoint has a single expected status, but in some cases (like a POST endpoint) you might get back a 201 for created or a 200 if the resource is already present. 如果端点具有单个预期状态,则此方法很好用,但是在某些情况下(如POST端点),您可能会获得201 (已创建)或200如果资源已存在)的信息。

Is there any way in frisby to check for on of several acceptable response status codes? frisby中有什么方法可以检查几个可接受的响应状态代码中的一个吗?

You can add extra method to Firsby lib , by creating local file xyz.coffee that will be required xyz = require "./xyz" there you can provide: 您可以通过创建本地文件xyz.coffee将额外的方法添加到Firsby lib ,在该文件中您可以提供以下内容: xyz = require "./xyz"

Frisby = Object.getPrototypeOf(frisby.create())

Frisby.expectStatuses = (statuses) ->
  @current.expects.push =>
    expect(statuses).toContain(@current.response.status)
  return this

Eventually you can always copy paste, the above, to your spec.coffee file After all, simply use in you script: 最终,您始终可以将上面的粘贴复制到您的spec.coffee文件中。毕竟,只需在脚本中使用:

.expectStatuses([200, 201])

It does the job perfectly. 它完美地完成了工作。

Ok, found an easy way to do this using the after() and native jasmine matching functionaly. 好的,找到了一种使用after()和本地茉莉花匹配功能的简单方法。

The general idea is since Frisby is asynchronous, you need to check the result after it's been tossed and caught. 通常的想法是,由于Frisby是异步的,因此您需要在扔掉并捕获结果之后检查结果。 Inside the raw result, you can manually verify the status code against a regular expression, which in this case is either 200 or 201: 在原始结果中,您可以根据正则表达式手动验证状态代码,在这种情况下,该代码可以为200或201:

 f = frisby.create(/*stuff....*/);
 // ...
 // ... 

 f.after( function(err, res, body) {

     //Look for either a 200 or 201
     expect(res.statusCode).toMatch(//20[0|1]/);

 });

 f.toss();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM