• Karma & Jasmine


    Steps to set up UnitTest for js

    1. According to the angularjs official document here, we select Karma, Jasmine,
      We can write our Test code accroding to the example of Jasmine.

    2. Karma is the Test Runner, Jasmine is the Test Framework. Now, we start our trip of js unit Testing.

    3. Prepare the environment.

    . install Karma
    we can follow these steps to install and configure karma
    step1. install npm, we can download the MSI, current, we chose the v0.12.9,
    step2. install karma, karma-Jasmine, karma-chrome-launcher karma-cli. using these commands:

    npm install karma --save-dev
    npm install karma-jasmine karma-chrome-launcher --save-dev
    npm install karma-core --save-dev   // we can install according to our requirements.
    

    step3. we can start to configure the file by running karma init. then we can configure the configure file with the default name: karma.conf.js.

    1. we can use Jasmine to writing our test case.

    To show an example,

    src.js
    
    function testController($scope) {
        var mySelf = this;
        $scope.test = false;
    
    }
    angular.module("App", ["ngDialog"])
            .controller("Test",
            {
                controller: testController,
                controllerAs:t
            });
    
    test.js
    
    describe("myHelloWorld", function () {
        beforeEach(module("App"));
        var tmp,scope;
        beforeEach(inject(function ($rootScope, $controller) {
            // scope = $rootScope.$new();
            scope = { test: true };
            tmp = $controller("Test", { $scope: scope });
        }))
        it("Test", function () {
            expect(scope.test).not.toEqual(true);
        })
    })
    
    karma.conf.js
    
    
    // Karma configuration
    // Generated on Tue Nov 01 2016 01:21:48 GMT-0400 (Eastern 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
        Be careful with the order of the files and the dependency relationship between them. as they will be loaded one by one.
        files: [
          'ERP_Dev/ERP_Dev/ERP/ERPWeb/Scripts/angular.js',
          'ERP_Dev/ERP_Dev/ERP/ERPWeb/Scripts/angular-mocks.js',
          'ERP_Dev/ERP_Dev/ERP/ERPWeb/Scripts/ngDialog.js',
          'ERP_Dev/ERP_Dev/ERP/ERPWeb/Scripts/ngDialog.min.js',
          'ERP_Dev/ERP_Dev/ERP/ERPWeb/App_Client/Components/Common/src.js',
          'ERP_Dev/ERP_Dev/ERP/ERPWeb/App_Client/Components/Orders/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,
    
        // Concurrency level
        // how many browser should be started simultaneous
        concurrency: Infinity
      })
    }
    
    
  • 相关阅读:
    软件仓库配置及编译http2.4及文件系统创建实例
    查找、打包、sed实例
    文件管理工具和基础脚本实例
    文件管理实例
    Linux系统中vim设置tab缩进为4个字符
    linux文件管理类命令及实例讲解
    文件元数据信息介绍及修改文件时间
    Linux发行版的系统目录名称命名规则以及用途
    回调和递归
    关于for循环的小案例
  • 原文地址:https://www.cnblogs.com/kongshu-612/p/6019761.html
Copyright © 2020-2023  润新知