[Unit Testing] What is Unit Testing. 1 - install Karma and test

What is Unit Testing:

Unit tests try to answer questions such as "Did I think about the logic correctly?" or "Does the sort funciton order the list in the right order?"

 

----------------------Install Karma--------------

1. using cmd, type:

npm install -g Karma
npm install -g karma-cli

 

2. In webstrom, open Terminal and type:

karma init

 

3. You will get a karma.config.js file,

put the name of the javascirpt files which you want to test into files array:

// Karma configuration
// Generated on Mon Sep 01 2014 12:35:38 GMT+0300 (FLE Daylight Time)

module.exports = function(config) {
  config.set({

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '',


    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['jasmine'],


    // list of files / patterns to load in the browser
    files: [
        "test/test.js"
    ],


    // list of files to exclude
    exclude: [
    ],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
    },


    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['progress'],


    // web server port
    port: 9876,


    // enable / disable colors in the output (reporters and logs)
    colors: true,


    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,


    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['Chrome'],


    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false
  });
};

 

4. In test.js we add some code:

describe("Hello",function(){
   it("should work",function(){
        expect(false).toBe(true)
    }) 
})

 

5. Click karma.config.js to "run" the file. We will get a error, because we expect false to be true.

"C:\Program Files\nodejs\node.exe" "F:\Program Files (x86)\JetBrains\WebStorm 8.0.4\plugins\js-karma\js_reporter\karma-intellij\lib\intellijRunner.js" --karmaPackageDir=C:\Users\Answer1215\node_modules\karma --serverPort=9876 --urlRoot=/

    Expected false to be true.
    Error: Expected false to be true.
        at null.<anonymous> (C:/Users/Answer1215/WebstormProjects/angular/Kama_Demo/test/test.js:3:23)


Process finished with exit code 0

 

6. Change the code in test.js:

describe("hello",function(){
    it("Should work",function(){
        expect(false).toBe(false);
    })
})

 

7. Run again , we pass the test.

"C:\Program Files\nodejs\node.exe" "F:\Program Files (x86)\JetBrains\WebStorm 8.0.4\plugins\js-karma\js_reporter\karma-intellij\lib\intellijRunner.js" --karmaPackageDir=C:\Users\Answer1215\node_modules\karma --serverPort=9876 --urlRoot=/

Process finished with exit code 0

 

posted @ 2014-09-01 17:58  Zhentiw  阅读(393)  评论(0)    收藏  举报