尝试使用带有Karma套件的expect js在JSON数组中断言JSON对象的存在

问题描述 投票:0回答:1

大家好,所以我正在编写我的redux异步操作的测试,我试图断言objet数组中存在一个对象但是我不能让测试成功运行,即使预期的副作用发生(将数据保存到firebase)

这是我的测试。

describe('Async Actions', () => {
        it('should upload supplier and dispatch ADD_SUPPLIER', (done)=> { /*Let Mocha know that this is asnyc test*/
          //mock a store
          const store = createMockStore({});

          //test supplier
          var testSupplier = {
            name: "new supplier",
            fact_sheet: "fact sheet",
            liability_sheet: "liability sheet",
            group: "group",
            service_lines: null,
            policies: null,
            rate_periods: null,
            rate_costs: null,
            contracts: null,
            cancellation_terms: null,
            deposit_terms: null,
            commission: 22,
            creation_date: moment().unix(),
            country: "SA",
            region: "Capetown",
            email: "[email protected]",
            telephone: "2797179",
            website: "ww.ww.ww",
            category: "cat",
            rating: 100
          };

          //Do the async action
          store.dispatch(actions.startAddSupplier(testSupplier.name, testSupplier.fact_sheet, testSupplier.liability_sheet, testSupplier.group,
             testSupplier.commission, testSupplier.country, testSupplier.region, testSupplier.email, testSupplier.telephone, testSupplier.website, testSupplier.category, testSupplier.rating)).then(
               () => {
                    /*Assertions*/
             const actions = store.getActions(); //returns array of all actions fired on store

             expect(actions).toInclude({
              type: 'ADD_SUPPLIER'
             });


                 /*done to show test is done*/
                 done();
               }).catch(done);//if done is called with args it is assumed the test has failed
        });
      });

这就是我在控制台中得到的错误

FAILED TESTS:
  Actions
    Supplier Actions
      Async Actions
        ✖ should upload supplier and dispatch ADD_SUPPLIER
          Chrome 53.0.2785 (Mac OS X 10.12.0)
        Error: Expected [ { supplier: { cancellation_terms: null, category: 'cat', commission: 22, contracts: null, country: 'SA', creation_date: 1476087460, deposit_terms: null, email: '[email protected]', fact_sheet: 'fact sheet', group: 'group', id: '-KThemyhqrmDHuF_PV2H', liability_sheet: 'liability sheet', name: 'new supplier', policies: null, rate_costs: null, rate_periods: null, rating: 100, region: 'Capetown', service_lines: null, telephone: '2797179', website: 'ww.ww.ww' }, type: 'ADD_SUPPLIER' }, { key: '-KThemyhqrmDHuF_PV2H', type: 'SET_CURR_SUPPLIER' } ] to include { type: 'ADD_SUPPLIER' }
            at Object.assert [as default] (eval at <anonymous> (/Users/zacck/Documents/www/jtx/app/tests/actions/actions.test.jsx:1231:2), <anonymous>:20:9)
            at Expectation.toInclude (eval at <anonymous> (/Users/zacck/Documents/www/jtx/app/tests/actions/actions.test.jsx:1129:2), <anonymous>:190:26)
            at eval (eval at <anonymous> (/Users/zacck/Documents/www/jtx/app/tests/actions/actions.test.jsx:47:2), <anonymous>:129:42)

我很确定我做错了什么我会感激任何寻找错误的帮助。

javascript json reactjs redux karma-runner
1个回答
0
投票

嗯,所以这里的问题似乎是你对单元测试的理解,你在这里测试的代码将一个异步方法调度到firebase实例。建议使用来自firebase实例的模拟响应来运行测试。这将允许您断言您的代码正确响应来自firebase的成功响应和失败响应,从而处理不愉快的路径。在这里看到这篇文章https://robots.thoughtbot.com/how-to-stub-external-services-in-tests

© www.soinside.com 2019 - 2024. All rights reserved.