0

I am setting up REST-API test within my codecept testing framework which uses integrated Chai.

After checking the very basic documentation on the subject in CodeceptJS documentation I can't seem to get my test to work.

const expect = require('chai').expect;

Feature('Checkout');

Scenario('Create a customer', async I => {
  const payload = ({
    first_name: 'Dummy title',
    last_name: 'Dummy description',
    email: 'john@test.com',
    locale: 'fr-CA',
    billing_address[first_name]: 'John',
    billing_address[last_name]: 'Doe',
    billing_address[line1]: 'PO Box 9999',
    billing_address[city]: 'Walnut',
    billing_address[state]: 'California',
    billing_address[zip]: '91789',
    billing_address[country]: 'US'
})
  const header = ({Content-Type: 'application/x-www-form-urlencoded'})

  const res = await I.sendPostRequest('https://mytesturl- 
   api.com/api/',payload,header);
  expect(res.code).eql(200);
});

I have put my payload and header in a variable for ease of use and readability.

But it doesn't work and keeps giving me Unexpected token [

4

1 回答 1

0

我想到了。

格式化有效负载的方式是字符串(参见下面的示例)

const expect = require('chai').expect;

Feature('Checkout');

Scenario('Create a customer', async I => {
 const payload = ({
  first_name: 'Dummy title',
  last_name: 'Dummy description',
  email: 'john@test.com',
  locale: 'fr-CA',
  'billing_address[first_name]': 'John',
  'billing_address[last_name]': 'Doe',
  'billing_address[line1]': 'PO Box 9999',
  'billing_address[city]': 'Walnut',
  'billing_address[state]': 'California',
  'billing_address[zip]': '91789',
  'billing_address[country]': 'US'
  })

const header = ({Content-Type: 'application/x-www-form-urlencoded'})

const res = await I.sendPostRequest('https://mytesturl-api.com/api/',payload,header);

expect(res.code).eql(200);
});
于 2019-01-18T13:25:52.923 回答