加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 23:28:36 所属栏目:百科 来源:网络整理
导读:我正在使用 Ruby版本2.0.0-p195运行Rails 3.2.13.我正在使用Jasmine-Rails gem版本0.4.5并启用资产管道.我想在CoffeeScript中编写我的规范,但我无法弄清楚如何命名文件并配置jasmine.yml,以便正确地拾取和解析规范. 这是我的jasmine.yml文件的内容: src_dir
我正在使用 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)的路径.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读