1

我在让 stubRequest 正常工作时遇到问题。这是我的代码:

it('should stub my request', (done) => {
    moxios.stubRequest('/authenticate', {
        status: 200
    })

    //here a call to /authenticate is being made
    SessionService.login('foo', 'bar')

    moxios.wait(() => {
        expect(something).toHaveHappened()
        done()
    })
})

这工作正常:

it('should stub my request', (done) => {
    SessionService.login('foo', 'bar')

    moxios.wait(async () => {
        let request = moxios.requests.mostRecent()

        await request.respondWith({
            status: 200
        })

        expect(something).toHaveHappened()

        done()
    })
})

第二种方法只是得到最后一次调用,我真的很希望能够明确地存根某些请求。

我正在用 Vue 运行 Jest。

4

2 回答 2

1

我带着类似的目标来到这里,并最终使用一种可能对其他人有帮助的不同方法解决了这个问题:

moxios.requests有一个方法.get()源代码moxios.requests),可让您根据 url获取特定请求。这样,如果您有多个请求,您的测试不需要以特定顺序发生的请求即可工作。

这是它的样子:

moxios.wait(() => {
  // Grab a specific API request based on the URL
  const request = moxios.requests.get('get', 'endpoint/to/stub');
  
  // Stub the response with whatever you would like
  request.respondWith(yourStubbedResponseHere)
    .then(() => {

      // Your assertions go here

      done();
    });
});

注意: 方法的名称.get()有点误导。它可以处理不同类型的 HTTP 请求。类型作为第一个参数传递,例如:moxios.requests.get(requestType, url)

于 2021-02-25T17:42:04.647 回答
0

如果您向我们展示这项服务,那就太好了。服务调用必须在 moxios 等待函数内部,外部必须单独为 axios 调用。我用 stubRequest 粘贴了一个简化的

describe('Fetch a product action', () => {
    let onFulfilled;
    let onRejected;

    beforeEach(() => {
        moxios.install();
        store = mockStore({});
        onFulfilled = sinon.spy();
        onRejected = sinon.spy();
    });

    afterEach(() => {
        moxios.uninstall();
    });

    it('can fetch the product successfully', done => {
            const API_URL = `http://localhost:3000/products/`;

            moxios.stubRequest(API_URL, {
                status: 200,
                response: mockDataSingleProduct
            });

            axios.get(API_URL, mockDataSingleProduct).then(onFulfilled);

            const expectedActions = [
                {
                    type: ACTION.FETCH_PRODUCT,
                    payload: mockDataSingleProduct
                }
            ];

            moxios.wait(function() {
                const response = onFulfilled.getCall(0).args[0];
                expect(onFulfilled.calledOnce).toBe(true);
                expect(response.status).toBe(200);
                expect(response.data).toEqual(mockDataSingleProduct);

                return store.dispatch(fetchProduct(mockDataSingleProduct.id))
                .then(() => {
                    var actions = store.getActions();
                    expect(actions.length).toBe(1);
                    expect(actions[0].type).toBe(ACTION.FETCH_PRODUCT);
                    expect(actions[0].payload).not.toBe(null || undefined);
                    expect(actions[0].payload).toEqual(mockDataSingleProduct);
                    expect(actions).toEqual(expectedActions);
                    done();
                });
            });
        });
})
于 2018-08-23T14:00:40.220 回答