Yeoman:用茉莉花和requirejs测试不起作用

时间:2014-06-24 17:48:54

标签: javascript requirejs gruntjs jasmine phantomjs

我通过Yeoman / Grunt / Phantomjs用Jasmine测试我的Backbone应用程序。当我在命令行中grunt jasmine时,我收到错误Warning: No specs executed, is there a configuration error? Use --force to continue. Aborted due to warnings.

即使是失败的测试也没有显示出来,我期待。

只有在规范中使用requirejs时才会出现此错误,但如果没有,则测试可以正常工作(即没有define())。因此,我猜测它与我如何使用Requirejs和Grunt配置Jasmine有关。 但我不知道在哪里以及如何解决这个问题。我用谷歌搜索但没有解决这个问题。

规范(至少这应该失败):

define(function(require) {
  var User = require('models/user');

  describe('Model : User', function() {
    var user;

    beforeEach(function() {
      user= new User;
    });

    it('should have a default string for name as "anon', function() {
      expect(user.get('name')).toEqual('anon');
    });
  });
});

使用Requirejs设置Jasmine:

require.config({
  shim: {
    jasmine: {
        exports: 'jasmine'
    },
    backbone: {
        deps: ['underscore', 'jquery'],
        exports: 'Backbone'
    }
  },
  paths: {
    jasmine: 'bower_components/jasmine/lib/jasmine-core/jasmine',
    'jasmine-html': 'bower_components/jasmine/lib/jasmine-core/jasmine-html',
    jquery: 'bower_components/jquery/dist/jquery',
    backbone: 'bower_components/backbone/backbone',
    underscore: 'bower_components/underscore/underscore',
    text: 'bower_components/requirejs-text/text',
    spec: 'spec'
  }
});

require(['jquery', 'jasmine-html'], function($, jasmine) {
  var jasmineEnv = jasmine.getEnv();
  jasmineEnv.updateInterval = 1000;

  var htmlReporter = new jasmine.HtmlReporter(),
      specs = [];

  jasmineEnv.addReporter(htmlReporter);

  jasmineEnv.specFilter = function(spec) {
    return htmlReporter.specFilter(spec);
  };

  $(function() {
    require(specs, function() {
      jasmineEnv.execute();
    });
  });
});

Gruntfile:

jasmine: {
    all:{
        src : '/scripts/{,*/}*.js',
        options: {
            keepRunner: true,
            specs : 'test/spec/*Spec.js',
            vendor : [
                '<%= yeoman.app %>/bower_components/jquery/dist/jquery.js',
                '<%= yeoman.app %>/bower_components/underscore/underscore.js',
                '<%= yeoman.app %>/bower_components/backbone/backbone.js',
                '.tmp/scripts/templates.js'
            ]
        }
    }
},
requirejs: {
    dist: {
        // Options: https://github.com/jrburke/r.js/blob/master/build/example.build.js
        options: {
            baseUrl: '<%= yeoman.app %>/scripts',
            optimize: 'none',
            paths: {
                'templates': '../../.tmp/scripts/templates',
                'jquery': '../../<%= yeoman.app %>/bower_components/jquery/dist/jquery',
                'underscore': '../../<%= yeoman.app %>/bower_components/underscore/underscore',
                'backbone': '../../<%= yeoman.app %>/bower_components/backbone/backbone'
            },
            preserveLicenseComments: false,
            useStrict: true,
            wrap: true
        }
    }
},...

请帮忙,谢谢

0 个答案:

没有答案