代码之家  ›  专栏  ›  技术社区  ›  Ceesiebird

在我的chai rest-api测试中,我应该使用什么格式作为有效负载和头文件?

  •  0
  • Ceesiebird  · 技术社区  · 6 年前

    我正在我的codecept测试框架中设置rest-api测试,该框架使用集成的chai。

    在检查了codeceptjs文档中关于这个主题的非常基本的文档之后,我似乎无法让测试正常工作。

    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);
    });
    

    为了方便使用和可读性,我把有效载荷和头放在一个变量中。

    但它不起作用,一直给我意想不到的纪念品[

    1 回复  |  直到 6 年前
        1
  •  0
  •   Ceesiebird    6 年前

    我知道了。

    设置有效负载格式的方法是将其作为字符串(参见下面的示例)

    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);
    });