ruby-on-rails – 如何在Jasmine-Rails中使用CoffeeScript规范

我正在使用 Ruby版本2.0.0-p195运行Rails 3.2.13.我正在使用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}"

这定义了spec文件名称模式.也就是说,零个或多个字符,后跟Spec或spec,以及js或coffee扩展名.

将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)的路径.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...