一尘不染

在业力单元测试期间如何修复图像的404警告

angularjs

我正在使用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
...

尽管它们什么也不做,但它们确实会在日志输出中增加噪音。有没有办法解决这个问题 ?(当然,无需更改业力的logLevel,因为我确实希望看到它们)


阅读 181

收藏
2020-07-04

共1个答案

一尘不染

那是因为您需要配置业力以进行加载,然后在需要时为它们提供服务;)

在您的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: [

],

// ...

您可以在这里查看更多信息:)

编辑: 如果您使用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

2020-07-04