Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.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

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
在ReactJS中模拟redux存储以进行测试的方法_Reactjs_Unit Testing_React Redux_Jestjs_Enzyme - Fatal编程技术网

在ReactJS中模拟redux存储以进行测试的方法

在ReactJS中模拟redux存储以进行测试的方法,reactjs,unit-testing,react-redux,jestjs,enzyme,Reactjs,Unit Testing,React Redux,Jestjs,Enzyme,我正在学习如何使用Jest和Ezyme测试ReactJS组件 我试图为异步操作创建者实现测试。我对模拟api使用了nock,对模拟存储使用了redux mock store 但是有没有一种方法可以在不使用任何附加库的情况下模拟存储呢。目前我可以使用jest、酶、反应测试库和nock 下面是使用redux模拟存储进行单元测试的代码。但是我们可以在没有redux模拟存储的情况下实现它吗 describe('Tests action creator', () => { let re

我正在学习如何使用Jest和Ezyme测试ReactJS组件

我试图为异步操作创建者实现测试。我对模拟api使用了nock,对模拟存储使用了redux mock store

但是有没有一种方法可以在不使用任何附加库的情况下模拟存储呢。目前我可以使用jest、酶、反应测试库和nock

下面是使用redux模拟存储进行单元测试的代码。但是我们可以在没有redux模拟存储的情况下实现它吗

describe('Tests action creator', () => {  
  
  let response ={
    "Details": [
      {
        "ID": "123",
        "Approval": 2,
        "Count": 1        
      }
    ]
  };

  axios.defaults.adapter = require('axios/lib/adapters/http')

  const middlewares = [thunk];
  const mockStore= configureMockStore(middlewares);
  const initialState:any = {Details :{} };

  const url='http://localhost:3000';
  let store:any ;
  beforeEach(() => {
   
  });
  afterEach(() => {
    // clear all HTTP mocks after each test
    nock.cleanAll();
  });
   
  it("test action creator with valid response",()=>
  {
    store = mockStore({});
    nock.disableNetConnect()
    nock(url)   
    .get('/Details.json')   
    .reply(200,response)
       
    const dispatchedStore =store.dispatch(actions.getDetails("123"));
   
    return dispatchedStore.then(() => {      
     expect(store.getActions()[0]).toEqual(actions.load(response.Details.filter((item:any) => item.transactionID === "123")));
  });
  })
})
请提供代码和您的尝试。