简体   繁体   中英

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

I'm using frisby v0.8.5 and jasimine-node . With frisby there's the following to assert for expected return http status code:

 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.

Is there any way in frisby to check for on of several acceptable response status codes?

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:

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:

.expectStatuses([200, 201])

It does the job perfectly.

Ok, found an easy way to do this using the after() and native jasmine matching functionaly.

The general idea is since Frisby is asynchronous, you need to check the result after it's been tossed and caught. Inside the raw result, you can manually verify the status code against a regular expression, which in this case is either 200 or 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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