2017-04-20 23 views
0

我在Redux上關注Dan Abramov的教程。 (https://egghead.io/lessons/javascript-redux-avoiding-object-mutations-with-object-assign-and-spreadTypeError:expect(...)。toEqual不是函數

在本課的9,他介紹了測試和使用expect.toEqual()

這是我的代碼:

var expect = require('chai').expect; 
var freeze = require('deep-freeze-node'); 

const testToggleTodo =() => { 
    const todoBefore = { 
     id: 0, 
     text: 'Learn Redux', 
     completed: false 
    }; 

    const todoAfter = { 
     id: 0, 
     text: 'Learn Redux', 
     completed: true 
    }; 

    expect(
     toggleTodo(todoBefore) 
     ).toEqual(todoAfter) 
} 

testToggleTodo(); 
console.log('All tests passed.') 

,我不斷收到錯誤:

.../react_redux/src/a.js:24 
     ).toEqual(todoAfter) 
     ^

TypeError: expect(...).toEqual is not a function 

什麼時我做錯了?我逐字複製他的代碼,但它會導致錯誤。

+0

真理的來源始終是官方文檔中未在別人的博客:http://chaijs.com/guide/styles/#expect – zerkms

回答

1

不確定提供的語法對於chai庫是否正確。 (我假設他們使用的是其他一些斷言庫)

平等應與expect功能檢查方法是

expect(toggleTodo(todoBefore)).to.equal(todoAfter); 

參考文獻:

+0

我想了解爲什麼他的代碼工作,我的不,不一定修改代碼,使其工作。 –

+0

@MorganAllen提供完整的代碼,包括進口。從視頻來看,「預期」並不明顯。 – zerkms

+0

他們沒有在視頻中顯示任何內容:-( –

0

使用了錯誤的包裝

不宜用柴,

進口預期在我的情況下,從「期望」

0

,使其工作我做了以下內容:

1. npm install --save expect        install package from npm 
2. import expect from 'expect';        import lib in file 
3. expect(toggleTodo(stateBefore, action)).toEqual(stateAfter);