【问题标题】:Karma test won't run业力测试不会运行
【发布时间】:2017-05-25 07:57:28
【问题描述】:

请帮忙。我是用业力测试的新手。当我运行“karma start 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();
  });
});

提前致谢,

威廉

【问题讨论】:

    标签: karma-runner


    【解决方案1】:
    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”,然后运行“ncu -u”以将所有软件包更新到最新版本。这可能有效,但我还没有测试过。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-01
      • 2017-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-10
      • 1970-01-01
      相关资源
      最近更新 更多