Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing Redux承诺中间件&x2B;Redux模拟存储测试_Unit Testing_Redux - Fatal编程技术网

Unit testing Redux承诺中间件&x2B;Redux模拟存储测试

Unit testing Redux承诺中间件&x2B;Redux模拟存储测试,unit-testing,redux,Unit Testing,Redux,我尝试使用redux mock store测试一个返回承诺的动作创建者 import promiseMiddleware from 'redux-promise-middleware'; import nock from 'nock'; import configureStore from 'redux-mock-store'; import { domain, port } from '../../config/environment'; import { GET_ITEMS_START,

我尝试使用redux mock store测试一个返回承诺的动作创建者

import promiseMiddleware from 'redux-promise-middleware';
import nock from 'nock';
import configureStore from 'redux-mock-store';
import { domain, port } from '../../config/environment';

import { GET_ITEMS_START,
         GET_ITEMS_SUCCESS } from '../../constants/items';
import { getItems } from './items';

const promise = promiseMiddleware({
  promiseTypeSuffixes: ['START', 'SUCCESS', 'ERROR']
});

describe('Get Items', () => {
    it('should create GET_ITEMS_SUCCESS action after successfully getting items', (done) => {
      nock(`${domain}:${port}`)
        .get('/api/items')
        .reply(200, {
          _id: '1',
          text: 'Make Eggs',
          completed: false
        });

      const expectedActions = [
        { type: GET_ITEMS_START },
        { type: GET_ITEMS_SUCCESS, payload: {
          data: {  _id: '1', text: 'Make Eggs', completed: false }
        }}
      ];

      const store = mockStore({}, expectedActions, done);
      store.dispatch(getItems());
    });
  });
这是我的动作创建者代码

export function getItems() {
  return {
    type: GET_ITEMS,
    payload: {
      promise: axios.get(`${domain}:${port}/api/items`)
    }
  };
}
但是结果是不匹配的,因为promise解析了一个深度嵌套的对象

  Error: Expected { payload: { config: { headers: {}, method: 'get', timeout: 0, transformRequest: [ [Function] ], transformResponse: [ [Function] ], url: 'http://localhost:3000/api/items', withCredentials: undefined }, data: { _id: '1', completed: false, text: 'Make Eggs' }, headers: {}, status: 200, statusText: 'OK' }, type: 'GET_ITEMS_SUCCESS' } to equal { payload: { data: { _id: '1', completed: false, text: 'Make Eggs' } }, type: 'GET_ITEMS_SUCCESS' }
  + expected - actual

   {
     "payload": {
  -    "config": {
  -      "headers": {}
  -      "method": "get"
  -      "timeout": 0
  -      "transformRequest": [
  -        [Function]
  -      ]
  -      "transformResponse": [
  -        [Function]
  -      ]
  -      "url": "http://localhost:3000/api/items"
  -      "withCredentials": [undefined]
  -    }
       "data": {
         "_id": "1"
         "completed": false
         "text": "Make Eggs"
       }
  -    "headers": {}
  -    "status": 200
  -    "statusText": "OK"
     }
     "type": "GET_ITEMS_SUCCESS"
   }
我显然不想将所有这些深层嵌套属性复制到我的测试套件中


有更好的方法吗?

如果您正在编写单元测试,您可能不需要调用实际的RESTAPI。相反,您可以模拟axios,让它返回一些不会太深的虚假数据

你能给我一个关于模仿axios的具体例子吗?这里有一个指南:这里有另一个:还有一个:谢谢。第一个环节看起来很有希望。我一试就回来报到