Karma测试不会运行

时间:2017-05-24 17:20:16

标签: karma-runner

请帮忙。我是业力测试的新手。当我跑步"业力开始karma.local.conf.js" Chrome浏览器窗口出现但测试无法运行。我认为我的app.js和我的test.js都没问题,我怀疑问题是我在package.json中加载的软件包版本不正确。我知道我还需要包括mocha和chai:

{
  "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();
  });
});

提前致谢,

威廉

1 个答案:

答案 0 :(得分: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.

另一种解决方案可能是运行&#34; npm install&#34;然后运行&#34; ncu -u&#34;将所有软件包更新到最新版本。这可能有用但我还没有测试过。