Busterjs + requirejs + backbone如何编写测试?

时间:2012-10-15 12:14:05

标签: backbone.js requirejs

组合busterjs + requirejs + backbone,我的项目结构没什么问题:

JS-SRC

- lib // jquery,require等

- 观点

- 模型

-app.js //需要配置并启动应用

js(编译与上面相同的结构)

测试

-buster.js

-require-config.js

-test-test.js

需要-config.js:

require.config({
  baseUrl: 'js-src/',
  paths: {
    jquery: 'lib/jquery',
    jplugins: 'lib/jquery.plugins',
    underscore: 'lib/underscore',
    backbone: 'lib/backbone'
  },
  shim: {
    'backbone': {
      deps: ['underscore', 'jplugins'],
      exports: 'Backbone'
    },
    'jplugins': {
      deps: ['jquery']
    }
  }
});

典型文件在lib中解除了:

define(function (require) {
    var $ = require('jquery'),
      Backbone = require('backbone'),
      otherElem = require('views/other'),
      View = Backbone.View.extend({
        el: '#el',

        initialize: function () {

        },

        showLinks: function (value) {

        },

        render: function ) {

        }

    });

    return View;
});

buster.js:

var config = module.exports;
config['browser-all'] = {
  autoRun: false,
  environment: 'browser',
  rootPath: '../',
  libs: [
    'js-src/lib/require.js',
    'test/require-config.js'
  ],
  sources: [
    'js-src/**/*.js'
  ],
  tests: [
    'test/*-test.js'
  ]
 // extensions: [
 //   require('buster-amd')
 // ]
};

测试test.js:

buster.spec.expose();
require(['views/View'], function (module) {
  describe("An AMD module", function () {
    it("should work", function () {
      expect(true).toEqual(true);
    });
  });
});

当我使用破坏者测试运行时,我得到:

Uncaught exception: ./js-src/lib/require.js:192 Error: Script error
http://requirejs.org/docs/errors.html#scripterror
TypeError: uncaughtException listener threw error: Cannot read property 'id' of undefined
    at Object.uncaughtException (/usr/local/lib/node_modules/buster/node_modules/buster-test-cli/lib/runners/browser/progress-reporter.js:49:50)
    at notifyListener (/usr/local/lib/node_modules/buster/node_modules/buster-core/lib/buster-event-emitter.js:37:31)
    at Object.emit (/usr/local/lib/node_modules/buster/node_modules/buster-core/lib/buster-event-emitter.js:101:17)
    at Object.emitCustom (/usr/local/lib/node_modules/buster/node_modules/buster-test-cli/lib/runners/browser/remote-runner.js:283:14)
    at /usr/local/lib/node_modules/buster/node_modules/buster-test-cli/lib/runners/browser/remote-runner.js:89:16
    at /usr/local/lib/node_modules/buster/node_modules/buster-test-cli/node_modules/buster-capture-server/lib/pubsub-client.js:79:47
    at Object.trigger (/usr/local/lib/node_modules/buster/node_modules/buster-test-cli/node_modules/buster-capture-server/node_modules/faye/node/faye-node.js:383:19)
    at Object.distributeMessage (/usr/local/lib/node_modules/buster/node_modules/buster-test-cli/node_modules/buster-capture-server/node_modules/faye/node/faye-node.js:666:30)
    at Object._deliverMessage (/usr/local/lib/node_modules/buster/node_modules/buster-test-cli/node_modules/buster-capture-server/node_modules/faye/node/faye-node.js:1065:20)
    at Object.<anonymous> (/usr/local/lib/node_modules/buster/node_modules/buster-test-cli/node_modules/buster-capture-server/node_modules/faye/node/faye-node.js:1004:12)
Firefox 16.0, Linux: 

如何使用该结构编写适当的测试?

2 个答案:

答案 0 :(得分:0)

如果您运行浏览器测试并检查控制台中的输出,这将有所帮助。其中的错误消息通常更具表现力。您还应该从buster配置中删除autoRun指令并重新启用“buster-amd”扩展。

答案 1 :(得分:0)

我昨天刚接触Buster.js,但我会添加以下4部分建议。

1。)在你的'buster.js'中取消注释“extensions:[require('buster-amd')]”

2。)从'require.config'

中删除'baseUrl'

3.。)明确设置libs的路径。例如“jplugins:'lib / jquery.plugins'”将成为“jplugins:'js-src / lib / jquery.plugins'”,这对于模型,集合,视图和其他文件也是必需的。 js-src /'目录。

require.config({
  paths: {
    jquery: 'js-src/lib/jquery',
    views: 'js-src/lib/views',
    somerootfile: 'js-src/somerootfile'

4.)将测试更改为......

describe('some test', function(run) {
    require(['models/your_model'], function(YourModel) {
        run(function() {
            it('should load', function() {
                var yourModel = new YourModel();
                    yourModel.set('cat', 'dog');
                expect(YourModel.get('cat')).toEqual('dog');
            });
        });
    });
});

问题似乎是'require.config'中的'baseUrl'混淆了Buster.js并告诉它不再尊重'buster.js'中设置的'rootPath'。