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

How to get data from response in 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 }

如果您以前遇到过这样的事情或者您有任何好的观点,请分享您的解决方法或提示。 我会感谢你的任何职位。 提前致谢。

这似乎不是与协议相关的问题 - 数据格式看起来您正在使用 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().