2016-11-08 72 views
0

我目前正在做一個反應項目與業力,酶作爲我的測試框架。我已經閱讀了來自https://github.com/airbnb/enzyme/blob/master/docs/api/shallow.md的酶文件。在本文檔中,它表示to.have.equal是檢查值的API。看下面的例子。什麼是正確的api用於酶

expect(wrapper.find(Foo)).to.have.length(3); 

但是當我寫我的測試用例時,karma抱怨未定義。經過一些搜索後,我必須使用下面的代碼而不是to.have.equal

expect(wrapper.find(Foo).length).toEqual(1); 

使用的酶版本是'^ 2.5.1'。我使用了錯誤的版本嗎?或者文件已過期?如果是這樣,我可以在哪裏找到正確的API文檔?

--EDIT1-- 下面是我用來運行噶命令:

./node_modules/karma/bin/karma start config/karma.conf.js --single-run --browsers PhantomJS 

下面是我的package.json文件中列出的依賴關係:

"devDependencies": { 
"autoprefixer": "^6.3.6", 
"babel-core": "^6.6.5", 
"babel-eslint": "^6.1.0", 
"babel-loader": "^6.2.4", 
"babel-plugin-transform-class-properties": "^6.11.5", 
"babel-preset-airbnb": "^2.1.1", 
"babel-preset-es2015": "^6.3.13", 
"babel-preset-react": "^6.3.13", 
"babel-preset-react-hmre": "^1.1.1", 
"babel-preset-stage-2": "^6.13.0", 
"classnames": "^2.2.5", 
"cross-env": "^1.0.7", 
"css-loader": "^0.23.1", 
"enzyme": "^2.5.1", 
"eslint": "^3.0.1", 
"eslint-plugin-babel": "^3.0.0", 
"eslint-plugin-react": "^5.2.2", 
"eventsource-polyfill": "^0.9.6", 
"extract-text-webpack-plugin": "^1.0.1", 
"file-loader": "^0.9.0", 
"html-webpack-plugin": "^2.10.0", 
"jasmine-core": "^2.4.1", 
"json-loader": "^0.5.4", 
"karma": "^0.13.19", 
"karma-babel-preprocessor": "^6.0.1", 
"karma-browserify": "^5.0.0", 
"karma-chrome-launcher": "^0.2.2", 
"karma-jasmine": "^0.3.7", 
"karma-mocha": "^0.2.1", 
"karma-phantomjs-launcher": "^1.0.0", 
"karma-sourcemap-loader": "^0.3.7", 
"karma-webpack": "^1.7.0", 
"koa": "^1.2.0", 
"koa-router": "^5.4.0", 
"koa-serve-index": "^1.1.1", 
"koa-static": "^2.0.0", 
"koa-views": "^4.1.0", 
"koa-webpack-dev-middleware": "^1.2.1", 
"koa-webpack-hot-middleware": "^1.0.3", 
"less": "^2.7.1", 
"less-loader": "^2.2.3", 
"loader-utils": "^0.2.16", 
"phantomjs-prebuilt": "^2.1.4", 
"postcss": "^5.1.2", 
"postcss-display-inline-block": "^1.0.0", 
"postcss-easysprites": "^0.1.7", 
"postcss-import": "^8.1.2", 
"postcss-less": "^0.14.0", 
"postcss-loader": "^0.9.1", 
"postcss-modules": "^0.5.0", 
"precss": "^1.4.0", 
"raphael": "^2.2.1", 
"react-css-modules": "^3.7.6", 
"react-hot-loader": "^1.3.0", 
"rimraf": "^2.4.3", 
"style-loader": "^0.13.0", 
"url-loader": "^0.5.7", 
"webpack": "^1.12.9", 
"webpack-dashboard": "^0.0.1", 
"webpack-dev-server": "^1.16.2", 
"webpack-hot-middleware": "^2.12.2" 

},

"dependencies": { 
    "axios": "^0.15.2", 
    "bootstrap": "^3.3.7", 
    "history": "^3.0.0", 
    "jquery": "^3.0.0", 
    "react": "^15.3.2", 
    "react-addons-test-utils": "^15.3.2", 
    "react-bootstrap": "^0.30.5", 
    "react-dom": "^15.0.1", 
    "react-form": "^0.0.2", 
    "react-paginate": "^2.2.3", 
    "react-redux": "^4.4.5", 
    "react-router": "^2.8.1", 
    "redux": "^3.6.0", 
    "redux-logger": "^2.7.4", 
    "redux-router": "^2.1.2", 
    "redux-thunk": "^2.1.0" 
    } 
+0

酶是一個測試框架和Karma渲染框架嗎? –

回答

0

Enzyme (and AFAIK the examples) was written with Mocha in mind:

酶最初的設計與摩卡的工作[...]

Karma example

it("contains spec with an expectation", function() { 
    expect(mount(<Foo />).find('.foo').length).toBe(1); 
}); 

由於docs state

酶與噶兼容,但通常需要一點點配置。

不要將測試運行器與渲染框架或斷言庫(如Chai)混合在一起。

+0

這是否意味着API'to.have.length'來自Mocha,'toEqual'來自Karma? –

+0

@趙一\t我不確定;對於摩卡我使用'柴',我不知道分界線在哪裏。 'toEqual'相同,不知道它是否是Karma或Enzyme。 –

+0

我已經檢查了兩個例子'酵素 - 摩卡'和'酶 - 示例-karma-webpack'。我發現在'enzyme-exmaple-mocha'的測試文件中,'chai''聲明有'import {expect},但在另一個例子中沒有這樣的狀態。這是不同的? –

相關問題