在CoffeeScript中定义的类在Jasmine规范中找不到 [英] Classes defined in CoffeeScript not found by Jasmine specs

查看:129
本文介绍了在CoffeeScript中定义的类在Jasmine规范中找不到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Rails 3.1后端建立一个backbone.js应用程式。我使用CoffeeScript编写主干类,并使用Jasmine(通过jasmine-headless-webkit)进行测试。



给定以下(部分)树:

 
├──app
│├──assets
││├──javascripts
│││└──视图
│││├──avia_view。 js.coffee
├──spec
│├──javascripts
││└──视图
││└──avia_view_spec.js.coffee

...我想要 avia_view_spec.js.coffee Avia.AviaView ,在 avia_view.js.coffee 中定义。



但是,我从运行 bundle exec jasmine-headless-webkit 获得以下输出:

 运行Jasmine规范... 
F

Avia.AviaView渲染创建一个新的MatricesView。 (/home/duncan/avia/spec/javascripts/views/avia_view_spec.js.coffee:10)
ReferenceError:找不到变量:Avia in / home / duncan / avia / spec / javascripts / views / avia_view_spec .js.coffee(line〜5)
ReferenceError:找不到变量:Avia in /home/duncan/avia/spec/javascripts/views/avia_view_spec.js.coffee(line〜10)

我的 jasmine.yml 包含以下内容:

  src_files:
- public / javascripts / prototype.js
- public / javascripts / effects.js
- public / javascripts / controls.js
- public / javascripts / dragdrop.js
- public / javascripts / application.js
- public / javascripts / ** / *。js



我需要告诉Jasmine加载 avia_view.js.coffee 但我不完全确定如何。在 jasmine.yml 中的 src_files 部分中添加显式引用似乎没有什么区别...



有人可以告诉我我在这里做错了吗?我怀疑它很简单...

解决方案

没有看到你的大部分代码,我怀疑它是CoffeeScript的函数包装(文档)。您需要确保要使用的所有符号都导出到您可以访问的位置(此处

CoffeeScript / SmoothCoffeeScript.html#toc-Chapter-11>这里是另一个冗长和理论,但关于这个主题的良好文档。


I am building a backbone.js app on a Rails 3.1 back-end. I'm using CoffeeScript to write the backbone classes, and Jasmine (via jasmine-headless-webkit) for testing.

Given the following (partial) tree:

.
├── app
│   ├── assets
│   │   ├── javascripts
│   │   │   └── views
│   │   │       ├── avia_view.js.coffee
├── spec
│   ├── javascripts
│   │   └── views
│   │       └── avia_view_spec.js.coffee

... I would expect avia_view_spec.js.coffee to know about Avia.AviaView, which is defined in avia_view.js.coffee.

However, I get the following output from running bundle exec jasmine-headless-webkit:

Running Jasmine specs...
F

Avia.AviaView render creates a new MatricesView . (/home/duncan/avia/spec/javascripts/views/avia_view_spec.js.coffee:10)
  ReferenceError: Can't find variable: Avia in /home/duncan/avia/spec/javascripts/views/avia_view_spec.js.coffee (line ~5)
  ReferenceError: Can't find variable: Avia in /home/duncan/avia/spec/javascripts/views/avia_view_spec.js.coffee (line ~10)

My jasmine.yml contains the following:

src_files:
  - public/javascripts/prototype.js
  - public/javascripts/effects.js
  - public/javascripts/controls.js
  - public/javascripts/dragdrop.js
  - public/javascripts/application.js
  - public/javascripts/**/*.js

I think I need to tell Jasmine to load the contents of avia_view.js.coffee but I'm not entirely sure how. Adding an explicit reference in the src_files section in jasmine.yml doesn't seem to make a difference ...

Could someone please tell me what I'm doing wrong here? I suspect it's something simple ...

解决方案

Without having seen to much of your code, I'd suspect it beacuse of CoffeeScript's function wrapping (docs). You need to ensure that all the symbols you want to use are exported to somewhere you can get at them (here is a thorough discussion about that).

Edit: here's another longish and theoretical but good documentation on this topic.

这篇关于在CoffeeScript中定义的类在Jasmine规范中找不到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆