如何从Pact的响应中获取数据

时间:2019-01-25 15:26:17

标签: reactjs pact

我在React项目中使用Pact进行api测试。但是我无法从响应中获得期望的数据。 我认为Pact配置没有错误。

这是我使用Pact的测试代码。

const { Pact } = require('@pact-foundation/pact');
const { getAllServices } = require('./index.ts');

describe('The API', () => {
    const res = [{
        id: '1',
        name: 'service1'
    },{
        id: '2',
        name: 'service2'
    },{
        id: '3',
        name: 'service3'
    }]
    describe("Services API testing", () => {
        beforeEach(() => {
            const interaction = {
                uponReceiving: "",
                withRequest: {
                    method:  'GET',
                    path:  '',
                    query:  '',
                    headers: {
                      Accept: 'application/json'
                    }
                },
                willRespondWith: {
                    status: 200,
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: res
                }
            };
            return provider.addInteraction(interaction);
        });

        it('returns all services', (done) => {
            return getAllServices()
                .then(response => {
                    expect(response.data).toEqual(res);
                    done()
                })
                .then(() => provider.verify());
        });
    });
});

这是我得到的数据格式。

Body {
      url: 'http://192.168.0.73:8080/services',
      status: 200,
      statusText: 'OK',
      headers: 
       Headers {
         _headers: 
          { 'content-type': [Array],
            'transfer-encoding': [Array],
            date: [Array],
            connection: [Array] } },
      ok: true,
      body: 
       PassThrough {
         _readableState: 
          ReadableState {
            objectMode: false,
            highWaterMark: 16384,
            buffer: [Object],
            length: 2743,
            pipes: null,
            pipesCount: 0,
            flowing: null,
            ended: false,
            endEmitted: false,
            reading: true,
            sync: false,
            needReadable: true,
            emittedReadable: true,
            readableListening: false,
            resumeScheduled: false,
            destroyed: false,
            defaultEncoding: 'utf8',
            awaitDrain: 0,
            readingMore: false,
            decoder: null,
            encoding: null },
         readable: true,
         domain: null,
         _events: 
          { end: [Object],
            prefinish: [Function: prefinish],
            unpipe: [Function: onunpipe],
            drain: [Function],
            error: [Function: onerror],
            close: [Object],
            finish: [Object] },
         _eventsCount: 7,
         _maxListeners: undefined,
         _writableState: 
          WritableState {
            objectMode: false,
            highWaterMark: 16384,
            finalCalled: false,
            needDrain: false,
            ending: false,
            ended: false,
            finished: false,
            destroyed: false,
            decodeStrings: true,
            defaultEncoding: 'utf8',
            length: 0,
            writing: false,
            corked: 0,
            sync: false,
            bufferProcessing: false,
            onwrite: [Function: bound onwrite],
            writecb: null,
            writelen: 0,
            bufferedRequest: null,
            lastBufferedRequest: null,
            pendingcb: 0,
            prefinished: false,
            errorEmitted: false,
            bufferedRequestCount: 0,
            corkedRequestsFree: [Object] },
         writable: true,
         allowHalfOpen: true,
         _transformState: 
          { afterTransform: [Function: bound afterTransform],
            needTransform: true,
            transforming: false,
            writecb: null,
            writechunk: null,
            writeencoding: 'buffer' } },
      bodyUsed: false,
      size: 0,
      timeout: 0,
      _raw: [],
      _abort: false }

如果您之前遇到过此类问题,或者您有任何好处,请分享您的修正或提示。 我会很感激您的任何帖子。 预先感谢。

1 个答案:

答案 0 :(得分:0)

这似乎不是与条约相关的问题-数据格式看起来像您正在使用fetch,但未正确使用API​​。

尝试交换:

return getAllServices()
            .then(response => {
                expect(response.data).toEqual(res);
                done()
            })

针对:

return getAllServices()
            .then(response => {
                expect(response.json()).toEqual(res);
                done()
            })

作为一种样式,Pact应该测试您的整个API层。为了坚持这种风格,我实际上将response.json()调用移到了getAllServices()内。