How to test Rails 3 Engines with Cucumber & Rspec?

浪子不回头ぞ 提交于 2019-12-02 18:15:01

Rails 3.1 (will) generate a pretty good scaffold for engines. I'd recommend using RVM to create a new gemset called edge and switch to it:

rvm gemset create edge
rvm use @edge

Then install edge rails:

git clone git://github.com/rails/rails.git
cd rails
rake install

From there, you can follow Piotr Sarnacki's mountable app tutorial, replacing calls such as:

bundle exec ./bin/rails plugin new ../blog --edge --mountable

With simply:

rails plugin new blog --mountable --full

The mountable option makes the application mountable, whilst the full option makes it an engine with tests already built-in. To test the engine, this generator generates a folder in test called dummy which contains a small Rails application. You can see how this is loaded in test/test_helper.rb.

Then it's up to you to massage the data to do what it needs to in order to work. I would recommend copying over the cucumber files from a standard rails g cucumber:install into the project and then messing about with it until it works. I've done this once before so I know it's possible, but I cannot find the code right now.

Let me know how you go.

I'll explain how I did it using as example the following gem: https://github.com/skozlov/netzke-core

The testing application. It is in netzke-core/test/rails_app. This app can be run independently, so I can also use it for manual testing or for playing around with new features if I like.

In order for the testing app to load the gem itself, I have the following in application.rb:

$:.unshift File.expand_path('../../../../lib', __FILE__)
require 'netzke-core'

Cucumber features. They are in netzke-core/features. In env.rb I have:

require File.expand_path(File.dirname(__FILE__) + '/../../test/rails_app/config/environment')

... which will load the testing application before executing the features.

Specs. These are in netzke-core/spec. In spec_helper.rb I have the following:

require File.expand_path("../../test/rails_app/config/environment", __FILE__)

... which will load the testing application before running the specs.

Running tests. This setup lets me run the tests from the root of the gem:

cucumber features

and

rspec spec

Factory Girl. Not for this particular gem, but I'm normally using factory_girl instead of fixtures (see, for example, a similar setup in https://github.com/skozlov/netzke-basepack).

A bit late to the party, but here is my strategy:

  1. Generating the rails plugin in 3.2:

    rails plugin new blog --mountable --full
    

    This creates test/dummy, containing the dummy rails app

  2. Add the specs to spec

  3. Move the dummy folder to spec (and optionally get rid of the other testfiles)

  4. Adapt specs/spec_helper.rb so it includes

    require File.expand_path("../.../config/environment", __FILE__)
    

    instead of

    require File.expand_path("../dummy/config/environment", __FILE__)
    
  5. Execute rails g cucumber:install. It will generate features folder a.o.

  6. Add

    ENV["RAILS_ROOT"] ||= File.expand_path(File.dirname(__FILE__) + '/../../spec/dummy')
    

    before

    require 'cucumber/rails'
    

    in features/support/env.rb

Now you have features and spec in the root of you project, while the dummy rails app is neatly tucked away under spec/dummy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!