用karma测试angularjs应用

来源:互联网 发布:淘宝产品拍照 编辑:程序博客网 时间:2024/06/05 20:36

介绍网上有很多就不赘述了

Karma的安装

sudo npm install karma -g

网上只给了这步,由于版本迭代会导致本地找不到KARMA命令。像很多模块的最新版本一样,只需再安装个客户端就OK了

sudo npm install karma-cli -g


再进到项目目录初始化

karma init
Which testing framework do you want to use ?Press tab to list possible options. Enter to move to the next question.> jasmineDo you want to use Require.js ?This will add Require.js plugin.Press tab to list possible options. Enter to move to the next question.> noDo you want to capture any browsers automatically ?Press tab to list possible options. Enter empty string to move to the next question.> Chrome>What is the location of your source and test files ?You can use glob patterns, eg. "js/*.js" or "test/**/*Spec.js".Enter empty string to move to the next question.>Should any of the files included by the previous patterns be excluded ?You can use glob patterns, eg. "**/*.swp".Enter empty string to move to the next question.>Do you want Karma to watch all the files and run the tests on change ?Press tab to list possible options.> yesConfig file generated at "/你的项目路径/karma.conf.js".

运行Karma

</pre><p><pre name="code" class="html">karma start karma.conf.js

以逆转字符串为例

home.js

function reverse(name){    return name.split("").reverse().join("");}

home.test.js

describe("A suite of basic functions", function() {    it("reverse word",function(){        expect("DCBA").toEqual(reverse("ABCD"));        expect("Conan").toEqual(reverse("nano"));    });});

启动后会在终端中看到

karma start karma.config.jsINFO [karma]: Karma v0.12.31 server started at http://localhost:9876/INFO [launcher]: Starting browser ChromeINFO [Chrome 41.0.2272 (Mac OS X 10.10.2)]: Connected on socket VqxM2bXSv64g2MlvUIK_ with id 29138942Chrome 41.0.2272 (Mac OS X 10.10.2) A suite of basic functions reverse word FAILEDExpected 'Conan' to equal 'onan'.    at Object.<anonymous> (/Users/Nathan/Workspaces/hnczb-web/test/home.test.js:4:25)Chrome 41.0.2272 (Mac OS X 10.10.2): Executed 1 of 1 (1 FAILED) (0 secs / 0.009 Chrome 41.0.2272 (Mac OS X 10.10.2): Executed 1 of 1 (1 FAILED) ERROR (0.014 secs / 0.009 secs)

注释掉
//expect("Conan").toEqual(reverse("nano"));
INFO [watcher]: Changed file "/Users/Nathan/Workspaces/hnczb-web/test/home.test.js".Chrome 41.0.2272 (Mac OS X 10.10.2): Executed 1 of 1 SUCCESS (0 secs / 0.003 secChrome 41.0.2272 (Mac OS X 10.10.2): Executed 1 of 1 SUCCESS (0.007 secs / 0.003 secs)

0 0
原创粉丝点击