如何模拟http响应

时间:2017-06-23 12:34:06

标签: javascript reactjs redux react-redux nock

尝试测试我的var type请求,但实际从服务器获取可以每秒更新的数据。所以,我使用var模拟请求,我的请求如下:

http

如果余额是我的数据,它看起来像:

nock

但是如果在服务器上这个数据会更新 - 我会收到另一个数据,而不是我在前端测试中写的数据。有什么方法可以解决我的问题?

我的测试用例:

nock('http://localhost:8080')
      .get('/api/getbalance')
      .reply(200, { body: balance });

UPD:

我使用此示例http://redux.js.org/docs/recipes/WritingTests.html#async-action-creators测试是否触发了操作。 问题出在{ "account": { "name": "Jack Torrance", "iban": "HTB0001234567", "balance": 3133.56 }, "currency": "EURO", "debitsAndCredits": [ { "from": "Wendy", "description": "Diner", "amount": 10.50, "date": "2016-01-10T09:20:00.000Z" }, 。在我import nock from 'nock'; import configureMockStore from 'redux-mock-store'; import thunk from 'redux-thunk'; import * as actions from './'; import * as types from '../constants'; const middlewares = [thunk]; const mockStore = configureMockStore(middlewares); describe('async actions', () => { afterEach(() => { nock.cleanAll(); }); it('creates FETCH_BALANCE_SUCCEESS when fetching balance has been done', () => { const store = mockStore({}); const balance = { account: { name: 'Jack Torrance', iban: 'HTB0001234567', balance: 3133.56, }, currency: 'EURO', debitsAndCredits: [ { from: 'Wendy', description: 'Diner', amount: 10.5, date: '2016-01-10T09:20:00.000Z', }, ], }; nock('http://localhost:8080') .get('/api/getbalance') .reply(200, { body: balance }); const expectedActions = [ { type: types.FETCH_BALANCE_REQUEST }, { type: types.FETCH_BALANCE_SUCCEESS, balance }, ]; return store.dispatch(actions.fetchBalance()).then(() => { expect(store.getActions()).toEqual(expectedActions); }); }); }); expect(store.getActions()).toEqual(expectedActions);actions.fetchBalance()不同之后,因为服务器上的余额发生了变化。

测试期望收到:

store.getActions()

但是因为服务器上的数据发生变化,我可以收到另一个数据:

expectedActions

0 个答案:

没有答案