jquery-jasmine无法加载json文件

时间:2015-10-19 23:07:20

标签: angularjs karma-jasmine jasmine-jquery

我已经看过这个问题,但似乎无法解决错误。我正在尝试使用jquery-jasmine返回一些假数据,并且在尝试加载json文件时我无法通过404错误。我正在使用自耕农来支撑我的角度吞噬项目。将json文件放在模块中,与spec文件一起放置是不是一个好主意?业力来自何处,这是我应该放置json文件的地方吗?我尝试使用jasmine.getJSONFixtures()。fixturesPath的绝对路径但仍然出错。我在夹具的路径中添加了“base”,因为我看到这个修复了另一个post中的错误,但它对我不起作用。有什么想法吗?

WARN [web-server]: 404: /base/src/app/home/home.fixture.json?_=1445293824062
PhantomJS 1.9.8 (Mac OS X) homeController should have some resultsets FAILED
    Error: JSONFixture could not be loaded: base/src/app/home/home.fixture.json (status: error, message: undefined)
    undefined

project/
  src/
    app/home
       home.controller.spec.js
       home.fixture.json
       ...
  gulp/unit-test.js

karma.conf.js
...

karma.conf.js:

'use strict';

module.exports = function(config) {

  config.set({
    basePath: '../',
    autoWatch : true,

    frameworks : ['jasmine'],

    browsers : ['PhantomJS'],

    plugins : [
        'karma-phantomjs-launcher',
        'karma-chrome-launcher',
        'karma-firefox-launcher',
        'karma-safari-launcher',
        'karma-jasmine'
    ],

    exclude : [

    ],

    files : [

        // fixtures
        {
            pattern: 'src/**/*.fixture.json', 
            watched: true, 
            served: true, 
            included: false
        }
    ]
  });
};

home.controller.spec.js

'use strict';

describe('homeController', function(){

    beforeEach(function() {
        module('homeModule');
        var $httpBackend, scope, homeController, storageFactory;
        module(function($provide) {
            $provide.service('storageFactory', storageFactory);
        });

        inject(function($rootScope, $controller, $injector) {
            $httpBackend = $injector.get('$httpBackend');
            jasmine.getJSONFixtures().fixturesPath = 'base/src/app/home';
            $httpBackend.whenGET('http://localhost:3000/my/api/home').respond(
                getJSONFixture('home.fixture.json')
            );
            scope = $rootScope.$new();
            homeController = $controller('homeController', {
                $scope: scope
            });
            storageFactory = $injector.get('storageFactory');
        });

    });


    it('should have some resultsets', function() {
        $httpBackend.flush();
        expect(scope.result_sets.length).toBe(59);
    });

});

2 个答案:

答案 0 :(得分:2)

我和yeoman-Angularjs-gulp和jquery-jasmine有相同的设置,但问题相同。 我用2方式解决了它,

  • 我的karma配置中未设置 basePath
  • json文件的路径最初并未由业力加载,因此它们永远不会被找到。

所以我在文件加载中添加了这行:

{pattern: path.join(conf.paths.src, '/mockjson/*.json'), 
watched: false, 
included: false, 
served: true}

这是由yeoman用2条额外线构建的基本业力配置:

var path = require('path');
var conf = require('./gulp/conf');
var _ = require('lodash');
var wiredep = require('wiredep');
function listFiles() {
  var wiredepOptions = _.extend({}, conf.wiredep, {
    dependencies: true,
    devDependencies: true
  });
  return wiredep(wiredepOptions).js
    .concat([
      path.join(conf.paths.src, '/app/**/*.module.js'),
      path.join(conf.paths.src, '/app/**/*.js'),
      path.join(conf.paths.src, '/**/*.spec.js'),
      path.join(conf.paths.src, '/**/*.mock.js'),
      path.join(conf.paths.src, '/**/*.html'),
      {pattern: path.join(conf.paths.src, '/mockjson/*.json'), watched: false, included: false, served: true}
    ]);
}

module.exports = function (config) {
  var configuration = {
    files: listFiles(),
    singleRun: true,
    autoWatch: false,
    basePath: '',
    frameworks: ['jasmine', 'angular-filesort'],
    angularFilesort: {
      whitelist: [path.join(conf.paths.src, '/**/!(*.html|*.spec|*.mock).js')]
    },
    ngHtml2JsPreprocessor: {
      stripPrefix: 'src/',
      moduleName: 'pagesBehindCouch'
    },
    browsers: ['PhantomJS'],
    plugins: [
      'karma-phantomjs-launcher',
      'karma-angular-filesort',
      'karma-jasmine',
      'karma-ng-html2js-preprocessor'
    ],
    preprocessors: {
      'src/**/*.html': ['ng-html2js']
    }
  };
  config.set(configuration);
};

我认为模式:' src / ** / * .fixing.json'可能不在您认为的src位置。 这真的对我很有帮助:Gunnar Hillert's Blog

答案 1 :(得分:0)

就我而言,我正在加载environment-configuration.json文件以引导AngularJS应用程序。 所以,我还必须指定代理设置。

proxies: {
  '/mockjson/': '/base/src/mockjson/',
}