如何在业力单元测试期间修复图像的 404 警告 [英] how to fix 404 warnings for images during karma unit testing

查看:25
本文介绍了如何在业力单元测试期间修复图像的 404 警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm unit testing one of my directives (angularjs) using grunt/karma/phantomjs/jasmine. My tests run fine

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

but I do get these 404s

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

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

Although they do nothing, they do add noise to the log output. Is there a way to fix this ? (without changing karma's logLevel of course, because I do want to see them)

推荐答案

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

That is because you need to configurate karma to load then serve them when requested ;)

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

In your karma.conf.js file you should already have defined files and/or patterns like :

// 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: [

],

// ...

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

You can have a look here for more info :)

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

EDIT : If you use a nodejs web-server to run your app, you can add this to karma.conf.js :

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

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

EDIT2 : If you don't use or want to use another server you can define a local proxy but as Karma doesn't provide access to port in use, dynamically, if karma starts on another port than 9876 (default), you will still get those annoying 404...

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

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

这篇关于如何在业力单元测试期间修复图像的 404 警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆