簡體   English   中英

為什么我的Karma Jasmine代碼覆蓋率始終為100%

[英]Why is my Karma Jasmine code coverage always 100%

我正在使用Grunt通過Karma / Jasmine運行我的單元測試,並且正在使用Karma進行代碼覆蓋,但是,代碼覆蓋率始終是100%,因為它似乎找不到文件,這是一些輸出:

PhantomJS 1.9.8(Mac OS X)信息:“正在開始測試...”

PhantomJS 1.9.8 (Mac OS X): Executed 2 of 2 SUCCESS (0.003 secs / 0.021 secs)
DEBUG [karma]: Run complete, exiting.
DEBUG [launcher]: Disconnecting all browsers
DEBUG [proxy]: failed to proxy /app/view/SearchForm.js?_dc=1428867938629 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/view/Main.js?_dc=1428867938628 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/view/ImageDetailView.js?_dc=1428867938629 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/view/ItemDetailView.js?_dc=1428867938629 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/controller/News.js?_dc=1428867938630 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/controller/Home.js?_dc=1428867938630 (browser hung up the socket)
DEBUG [coverage]: Writing coverage to /appdir/coverage/PhantomJS 1.9.8 (Mac OS X)
----------|-----------|-----------|-----------|-----------|
File      |   % Stmts |% Branches |   % Funcs |   % Lines |
----------|-----------|-----------|-----------|-----------|
----------|-----------|-----------|-----------|-----------|
All files |       100 |       100 |       100 |       100 |
----------|-----------|-----------|-----------|-----------|

DEBUG [launcher]: Process PhantomJS exited with code 0
DEBUG [temp-dir]: Cleaning temp dir

Done, without errors.

如您所見,單元測試可以找到要測試的正確文件,但是代碼覆蓋插件找不到相同的文件。 我認為這是因為我使用grunt-contrib-connect而不是內置的Karma Web服務器。 當我使用內置的業力服務器時,單元測試也不起作用,因為它們也找不到文件。 這是我的karma.conf:

module.exports = function (config) {
    config.set({
        basePath: '',

        frameworks: [ 'jasmine' ],

        files: [
            'touch/sencha-touch-all.js',
            'setup.js',
            'app.js',
            {
                pattern: 'app/**/*.js',
                watched: true,
                included: false,
                served: true
            },
            'tests/**/*.js'
        ],

        proxies: {
            '/': 'http://localhost:9000/'
        },

        preprocessors: {
            'app/**/*.js': ['coverage']
        },
        reporters: ['junit', 'progress', 'coverage'],

        coverageReporter: {
            type: 'text'
        },

        port: 9876,
        colors: true,
        logLevel: config.LOG_DEBUG,
        autoWatch: true,
        browsers: [ 'PhantomJS' ],
        captureTimeout: 60000,
        singleRun: false
    });
};

我認為在代碼覆蓋范圍開始之前,grunt-contrib-connect服務器正在退出。有人知道為什么或如何停止此操作嗎? 甚至更好的是,沒有人知道為什么內置的業力服務器不起作用。 當我使用內置的業力服務器時,會發生以下情況:

DEBUG [proxy]: failed to proxy /app/model/Image.js?_dc=1428868670561 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/model/Image.js?_dc=1428868670561 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/model/Image.js?_dc=1428868670561 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/model/Image.js?_dc=1428868670561 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/model/Image.js?_dc=1428868670561 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/model/Image.js?_dc=1428868670561 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/model/Image.js?_dc=1428868670561 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/model/Image.js?_dc=1428868670561 (browser hung up the socket)
DEBUG [proxy]: failed to proxy /app/model/Image.js?_dc=1428868670561 (browser hung up the socket)

等等....

您的問題可能是由於文件設置在coverage插件期望的位置沒有可用的文件所致,例如, 包括在內:錯誤的設置需要從腳本加載器(如require.js)中進行手動加載。

    files: [
        'touch/sencha-touch-all.js',
        'setup.js',
        'app.js',
        {
            pattern: 'app/**/*.js',
            watched: true,
            included: false,
            served: true
        },
        'tests/**/*.js'
    ],

嘗試在沒有手動加載程序的情況下進行文件設置,然后讓業力加載文件,看看是否適合您。

    files: [
        'touch/sencha-touch-all.js',
        'setup.js',
        'app.js',
        'app/**/*.js',
        'tests/**/*.js'
    ],

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM