2

我有一个使用 RequireJS 加载的项目,我想测试模型。

我有以下测试:

require(['../../js/models/lead_sharing'], function(Lead_sharing_Model) {
    describe('when instantiated', function () {
        it('should exhibit attributes', function() {
            var test = new Lead_sharing_Model({
                attribute : 3
            });
            expect(test.get('attribute')).toEqual(3);
        });
    });
});

但我有一个错误“失败并出现错误:SyntaxError: Unexpected token”...

实际上,我不知道是否可以使用 Jasmine 测试主干/requireJs 项目。确实,我如何在不需要配置的情况下包含我的视图/模型...(例如路径的定义...)

谢谢

Ps:只是一个小的编辑来指定我想用aa js cmd进行测试。在浏览器中没有。:)

4

1 回答 1

4

当然可以用 jasmine 测试一个骨干网/requirejs 项目。将应用程序使用的相同需求配置拉入驱动单元测试的 html 页面。例如:

<!doctype html>
<html lang="en">
<head>
  <link rel="stylesheet" href="vendor/jasmine.css">
</head>

<body>
  <script src="vendor/jasmine.js"></script>
  <script src="vendor/jasmine-html.js"></script>
  <script src="../assets/js/libs/jquery.js"></script>
  <script src="../assets/js/libs/underscore.js"></script>
  <script src="../assets/js/libs/backbone.js"></script>

  <!-- This points to the require config that is also used by the main app. -->
  <script data-main="../app/config" src="../assets/js/libs/require.js"></script>

  <script>
    require({ paths: { spec: "../test/spec" } }, [
      // Pull in all your modules containing unit tests here.
      "spec/lead-sharing-specs"
    ], function() {
      jasmine.getEnv().addReporter(new jasmine.TrivialReporter());
      jasmine.getEnv().execute();
    });
  </script>
</body>
</html>

由于您想在浏览器之外运行它,请查看PhantomJSgruntgrunt-jasmine-task

于 2012-07-14T04:32:20.297 回答