我正在使用
Ruby版本2.0.0-p195运行Rails 3.2.13.我正在使用Jasmine-Rails gem版本0.4.5并启用资产管道.我想在CoffeeScript中编写我的规范,但我无法弄清楚如何命名文件并配置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)的路径.