【问题标题】:Unable to get Moxios stubRequest to work无法让 Moxios stubRequest 工作
【发布时间】:2019-01-29 16:20:35
【问题描述】:

我在让 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。

【问题讨论】:

    标签: axios jestjs moxios


    【解决方案1】:

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

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

    它是这样的:

    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)

    【讨论】:

      【解决方案2】:

      如果您能向我们展示这项服务,那就太好了。服务调用必须在 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();
                      });
                  });
              });
      })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-13
        • 1970-01-01
        • 2020-02-23
        • 2014-12-26
        • 2017-12-22
        • 2012-02-28
        • 2015-12-01
        • 2012-01-29
        相关资源
        最近更新 更多