0
你好,所以我正在爲我的redux異步動作編寫測試,並且我試圖斷言對象存在於objet數組中,但我無法獲得即使預期的副作用發生,測試也能成功運行(將數據保存到Firebase)試圖在JSON數組中使用期望js與Karma套件來聲明JSON對象的存在
這是我的測試。
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)
我敢肯定,我做錯了什麼我想任何感謝所有幫助找到錯誤。
您的測試'testSupplier'沒有'id'屬性。 –