我正在使用 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/'
};
关于javascript - 如何在 karma 单元测试期间修复图像的 404 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21067710/