ruby-on-rails - 如何将 CoffeeScript 规范与 Jasmine-Rails 一起使用

标签 ruby-on-rails ruby coffeescript tdd jasmine

我正在使用 Ruby 版本 2.0.0-p195 运行 Rails 3.2.13。我正在使用启用了 Assets 管道的 Jasmine-Rails gem 版本 0.4.5。我想在 CoffeeScript 中编写我的规范,但我一直无法弄清楚如何命名文件和配置 jasmine.yml,以便规范能够被正确地提取和解析。

这是我的 jasmine.yml 文件的内容:

src_dir: "app/assets/javascripts"

src_files:
 - "application.{js,coffee}"

spec_dir: spec/javascripts

helpers:
  - "helpers/**/*.{js,coffee}"

spec_files:
  - "**/*[Ss]pec.{js,coffee}"

我试过将文件命名为 my_spec.js.coffee。那是行不通的。当然 my_spec.js 命名约定工作得很好。我还尝试弄乱 jasmine.yml 中的 spec_files 值,但没有成功。

提前感谢任何遇到过类似问题、解决了问题并能帮助我编写稍微不那么乏味的规范的人。

最佳答案

来自 Jasmine-Rails's Github homepage :

The jasmine-rails gem fully supports the Rails asset pipeline which means you can:

  • use coffee_script or other Javascript precompilers for source or test files
  • use sprockets directives to control inclusion/exclusion of dependent files
  • leverage asset pipeline search paths to include assets from various sources/gems

If you choose to use the asset pipeline support, many of the jasmine.yml configurations become unnecessary and you can rely on the Rails asset pipeline to do the hard work of controlling what files are included in your testsuite.

# minimalist jasmine.yml configuration when leveraging asset pipeline
spec_files:
  - "**/*[Ss]pec.{js,coffee}"

这定义了规范文件的名称模式。也就是说,零个或多个字符,后跟 Specspec,并带有 jscoffee 扩展名。

将 Jasmine-Rails 路由配置添加到 config/routes.rb

mount JasmineRails::Engine => "/specs" if defined?(JasmineRails)

现在您可以在 spec/javascripts/my_spec.coffee 中编写规范来测试 Foo。

describe 'Foo', ->
  it 'does something', ->
    expect(1 + 1).toBe(2)

你的 RoR 项目结构部分应该是这样的:

RailsApp/
|---app/
|---config/
|   |---routes.rb
|---...
|---spec/
    |---javascripts/
        |---my_spec.coffee
        |---support/
            |---jasmine.yml

您可以使用以下方法测试规范:

bundle exec rake spec:javascript

或者您可以启动 Rails 服务器并检查 Jasmine-Rails 的路径 (/specs)。

关于ruby-on-rails - 如何将 CoffeeScript 规范与 Jasmine-Rails 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17173506/

相关文章:

list-comprehension - 枚举并在Coffeescript中压缩?

ruby-on-rails - 如何验证在 rails 中包含数组内容

ruby-on-rails - 如何使用 RoR 助手添加下拉菜单/选择?

javascript - 使用 jquery UI 确认表单提交

iphone - 如何减少 iphone 的 sqlite3 数据库的大小?

javascript - d3.js 数据绑定(bind)需要唯一键来存储 csv 文件中的重复条目

javascript - bacon.js 中的控制流,如何在给定时间做某事

ruby-on-rails - 如何使用 Mongoid 执行 runCommand?

ruby - 整理猴子补丁

ruby-on-rails - 在 Rails Controller 规范中模拟外部类