简体   繁体   中英

Testing against multiple possible responses (API, Frisby 2)

I'm trying to write tests for a REST API that could return one of two http status codes, both of which I consider to be correct.

For some reason I'm using Frisby 2, the library that's so easy, you don't even need documentation to use it!

Anyway, in Frisby's own unit tests, it compares against a regex:

it('expectHeader should match with regex', function(doneFn) {
  mocks.use(['getUser1']);

  frisby.fetch(testHost + '/users/1')
    .expect('header', 'Content-Type', /json/)
    .done(doneFn);
});

Great! So I'll use that:

frisby.fetch('/rest/endpoint', {
    method: 'POST',
    body: form
})
.expect('status', /201|429/)

Oh dear

assert.strictEqual(received, expected)

Expected value to be (operator: ===):
  /201|429/
Received:
  429

Message:
  HTTP status /201|429/ !== 429

Difference:

  Comparing two different types of values. Expected regexp but received number.

What am I doing wrong?

Looks like you can fallback to Jasmine to do this sort of thing:

frisby.fetch('/rest/endpoint', {
    method: 'POST',
    body: form
})
.then(function (res) {
    expect(res.status === 201 || res.status === 429).toBe(true);
})

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