2017-05-24 195 views
0

請幫忙。我是新的測試與業力。當我運行「karma start karma.local.conf.js」時,Chrome瀏覽器窗口出現,但測試無法運行。我認爲我的app.js和我的test.js都可以,我懷疑問題在於我在package.json中加載的包的版本不正確。我知道我還需要包括摩卡和薛寶釵:Karma測試不會運行

{ 
    "devDependencies": { 
    "browserify": "10.2.3", 
    "gulp": "3.8.11", 
    "gulp-browserify": "0.5.1", 
    "karma": "0.12.16", 
    "karma-chai": "0.1.0", 
    "karma-chrome-launcher": "0.1.4", 
    "karma-mocha": "0.1.4", 
    "http-status": "0.1.8", 
    "underscore": "1.5.2" 
} 
} 

這裏是我的karma.local.conf.js文件:

module.exports = function(config) { 
    config.set({ 
    files: [ 
     'http://code.jquery.com/jquery-1.11.3.js', 
     'https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.js', 
     // For ngMockE2E 
     'https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular-mocks.js', 
     './app.js', 
     './test.js' 
    ], 
frameworks: ['mocha', 'chai'], 
browsers: ['Chrome'], 
proxies : { 
    '/': 'http://localhost:3000' 
} 
}); 
}; 

我也可以張貼app.js和test.js如果需要的話,但我認爲他們沒問題。我認爲這個問題在package.json中,並且得到我需要的npm包的正確版本。

這裏是我的app.js:

var app = angular.module('myApp', ['ng']); 

app.directive('userMenu', function() { 
    return { 
    controller: 'MyHttpController', 
    template: '<div class="user" ng-show="user">' + 
      ' Current User: {{user.profile.username}}' + 
      '</div>' + 
      '<div ng-show="!user">' + 
      ' <a href="/auth/facebook">' + 
      ' Log In' + 
      ' </a>' + 
      '</div>' 
    } 
}); 

app.controller('MyHttpController', function($scope, $http) { 
    $http.get('/api/v1/me').success(function(data) { 
    $scope.user = data.user; 
    }); 
}); 

這裏是我的test.js:

describe('Nav Bar', function() { 
    var injector; 
    var element; 
    var scope; 
    var compiler; 
    var httpBackend; 

    beforeEach(function() { 
    injector = angular.injector(['myApp', 'ngMockE2E']); 
    intercepts = {}; 

    injector.invoke(function($rootScope, $compile, $httpBackend) { 
     scope = $rootScope.$new(); 
     compiler = $compile; 
     httpBackend = $httpBackend; 
    }); 
    }); 

    it('shows logged in users name', function(done) { 
    httpBackend.expectGET('/api/v1/me').respond({ 
     user: { profile: { username: 'John' } } 
    }); 

    element = compiler('<user-menu></user-menu>')(scope); 
    scope.$apply(); 

    httpBackend.flush(); 
    assert.notEqual(element.find('.user').css('display'), 'none'); 
    assert.equal(element.find('.user').text().trim(), 'Current User: John'); 
    done(); 
    }); 
}); 

由於提前,

威廉

回答

0
Here is what I did to get my tests to run: 

1. Run "npm install <package name> without a version number for each package in my package.json file 
2. Run "karma init" to create a new config file. By default this command creates karma.conf.js 
3. Modify karma.conf.js with the information from karma.local.conf.js 
4. Run "karma start". By default this command looks for a file named karma.conf.js (I had already run "npm install -g karma-cli" so I was able to run "karma start" without specifying a path 
5. Next I will run "npm ls" to see what package versions were installed and create a new version of package.json for future projects. 

另一種解決方案可能會運行「npm install」a然後運行「ncu -u」將所有軟件包更新到最新版本。這可能工作,但我沒有測試它。