javascript - 如何在 karma 单元测试期间修复图像的 404 警告

标签 javascript unit-testing angularjs http-status-code-404 karma-runner

我正在使用 grunt/karma/phantomjs/jasmine 对我的一个指令 (angularjs) 进行单元测试。我的测试运行良好

describe('bar foo', function () {
    beforeEach(inject(function ($rootScope, $compile) {
        elm = angular.element('<img bar-foo src="img1.png"/>');
        scope = $rootScope.$new();
        $compile(elm)();
        scope.$digest();
    }));
    ....
});

但我确实得到了这些 404

WARN [web-server]: 404: /img1.png
WARN [web-server]: 404: /img2.png
...

虽然它们什么都不做,但它们确实给日志输出增加了噪音。有没有办法来解决这个问题 ? (当然不改变 karma 的 logLevel,因为我确实想看到它们)

最佳答案

那是因为你需要配置 karma 来加载然后在请求时为它们提供服务 ;)

在您的 karma.conf.js 文件中,您应该已经定义了文件和/或模式,例如:

// list of files / patterns to load in the browser
files : [
  {pattern: 'app/lib/angular.js', watched: true, included: true, served: true},
  {pattern: 'app/lib/angular-*.js', watched: true, included: true, served: true},
  {pattern: 'app/lib/**/*.js', watched: true, included: true, served: true},
  {pattern: 'app/js/**/*.js', watched: true, included: true, served: true},
  // add the line below with the correct path pattern for your case
  {pattern: 'path/to/**/*.png', watched: false, included: false, served: true},
  // important: notice that "included" must be false to avoid errors
  // otherwise Karma will include them as scripts
  {pattern: 'test/lib/**/*.js', watched: true, included: true, served: true},
  {pattern: 'test/unit/**/*.js', watched: true, included: true, served: true},
],

// list of files to exclude
exclude: [

],

// ...

可以看看here了解更多信息:)

编辑:如果您使用 nodejs 网络服务器来运行您的应用程序,您可以将其添加到 karma.conf.js 中:

proxies: {
  '/path/to/img/': 'http://localhost:8000/path/to/img/'
},

EDIT2:如果您不使用或不想使用其他服务器,您可以定义一个本地代理,但是如果 karma 在另一个服务器上启动,Karma 不提供对正在使用的端口的动态访问端口不是 9876(默认),你仍然会得到那些烦人的 404...

proxies =  {
  '/images/': '/base/images/'
};

相关问题:https://github.com/karma-runner/karma/issues/872

关于javascript - 如何在 karma 单元测试期间修复图像的 404 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21067710/

相关文章:

javascript - Google Charts 图表区域超出范围

java - 使用 Junit 进行数据驱动测试

javascript - 双向数据绑定(bind)不适用于 Internet Explorer 9 中的自定义指令

javascript - AngularJS - 简单的部分不加载

c# - 在 Unity 中注册用于拦截的实例

javascript - AngularJS-每个路由和 Controller 中的登录和身份验证

javascript - Paypal 支付转账确认API

Javascript 不能始终在 android webview 上工作。有时它起作用,大多数时候它不起作用

javascript - 如何使用本地存储在新的html页面中显示文本?

unit-testing - grails test-app @grails.test.mixin.TestFor 中的属性 'value' 只能使用类和闭包