Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add example group for features #632

Closed
wants to merge 3 commits into from

4 participants

@jnicklas

This fixes various issues with the integration between RSpec and Capybara 2.0.

  • It includes Route helpers, these were previously unavailable, due to the fact that the only reason they are available in request spec is that those delegate via method_missing to an IntegrationTest instance.

  • It adds the :feature type to all features. This way people can include module into :type => :feature examples and they will work when their specs are places in this folder.

David previously commented that he didn't want to add Capybara specific behaviour outside of rspec/rails/capybara. I was careful to avoid doing that. The feature example group does not explicitly include Capybara, instead, Capybara is included due to Capybara itself hooking into :type => :feature. This is much cleaner than the way it was before.

I tested this against both a freshly generated Rails app, as well as an existing, rather large project, both worked well.

@jnicklas jnicklas Add example group for features
This fixes various issues with the integration between RSpec and Capybara 2.0.

- It includes Route helpers, these were previously unavailable, due to the fact that the only reason they are available in request spec is that those delegate via method_missing to an IntegrationTest instance.

- It adds the `:feature` type to all features. This way people can include module into `:type => :feature` examples and they will work when their specs are places in this folder.

David previously commented that he didn't want to add Capybara specific behaviour outside of `rspec/rails/capybara`. I was careful to avoid doing that. The feature example group does not explicitly include Capybara, instead, Capybara is included due to Capybara itself hooking into `:type => :feature`. This is much cleaner than the way it was before.

I tested this against both a freshly generated Rails app, as well as an existing, rather large project, both worked well.
e129450
@jnicklas

Saw that Travis failed for earlier Rails versions, so I pushed a fix.

@alindeman
Collaborator

I initially thought a FeatureExampleGroup was a good idea, then waffled after talking to @dchelimsky, but I think URL helpers are a compelling reason to create one.

If capybara is not present, FeatureExampleGroup is still usable as a bare bones example group .. and with route helpers. I'm not sure how likely it is for someone to use it this way, but the introduction of another type of example group that only includes capybara if it's present doesn't worry me much.

Unless there are strong objections, I will revert my earlier work in 96e1c33 and go with @jnicklas's.

@jnicklas jnicklas referenced this pull request in jnicklas/capybara
Merged

set rspec type to :feature instead #809

@dchelimsky
Owner

@alindeman I have no objection.

@jnicklas thanks for the pull request - really appreciate the collaborative effort here. Cheers!

@dchelimsky
Owner

One additional consideration (which can be addressed in a separate pull/commit) is that FeatureExampleGroup is currently only meaningful when Capybara is loaded - there are no functions for simulating requests otherwise - so I think we probably want to raise an error if Capybara is not loaded. WDYT?

@jnicklas

It seems to me like there is no harm if someone wants to use the FeatureExampleGroup without Capybara. Especially since rspec-rails tries to load capybara, so if it is available, it will be loaded anyway. Someone could use selenium-webdriver directly, for example. I don't see why that should cause a failure.

@dchelimsky
Owner

My concern is confusion caused by creating a file in spec/features without Capybara installed or loaded and not understanding why there is no get/post/put/delete/head or visit.

@jnicklas

@dchelimsky: would this be an acceptable solution: jnicklas@jnicklas:feature-example-group...jnicklas:with-fake-capybara Most people are going to call visit first, so hinting at Capybara not being loaded there might be sufficient.

If you approve, I can send a pull request for that too, but let's get this one merged, so that we can move forward!

@dchelimsky
Owner

@jnicklas works for me. @alindeman, wdyt?

@alindeman
Collaborator

I like it. I'm pull that in, as well as fixing my quasi-conflicting changes, now. Thanks so much @jnicklas and other Capybara folks for working this, and I apologize about the delay.

@alindeman alindeman referenced this pull request from a commit in alindeman/rspec-rails
@alindeman alindeman Revert "Capybara feature specs may use Rails URL helpers"
This reverts commit 96e1c33.

* We'll instead use an implementation that adds a proper
  FeatureExampleGroup (see #632)
1152131
@alindeman
Collaborator

@jnicklas, would you mind performing similar tests against the code I just pushed to https://github.com/alindeman/rspec-rails/tree/with-fake-capybara? I kept much of your implementation, but tweaked it a bit based on the Rails code I originally wrote it against.

If it works and you feel good about it, I'll merge that.

Feel free to call me out on anything you see that might be an issue.

@jnicklas

@alindeman looks good to me! Good catch on the host stuff, I didn't consider that at all.

@alindeman
Collaborator

Committed with 0fda98b. I will spend some time writing up more docs on this soon, but hopefully that helps remove road blocks. If not, ping me!

@alindeman alindeman closed this
@alindeman
Collaborator

I just pushed some new docs for feature specs. Reviews welcomed: 6e68bdd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 5, 2012
  1. @jnicklas

    Add example group for features

    jnicklas authored
    This fixes various issues with the integration between RSpec and Capybara 2.0.
    
    - It includes Route helpers, these were previously unavailable, due to the fact that the only reason they are available in request spec is that those delegate via method_missing to an IntegrationTest instance.
    
    - It adds the `:feature` type to all features. This way people can include module into `:type => :feature` examples and they will work when their specs are places in this folder.
    
    David previously commented that he didn't want to add Capybara specific behaviour outside of `rspec/rails/capybara`. I was careful to avoid doing that. The feature example group does not explicitly include Capybara, instead, Capybara is included due to Capybara itself hooking into `:type => :feature`. This is much cleaner than the way it was before.
    
    I tested this against both a freshly generated Rails app, as well as an existing, rather large project, both worked well.
Commits on Nov 6, 2012
  1. @jnicklas
  2. @jnicklas
This page is out of date. Refresh to see the latest.
View
4 lib/rspec/rails/example.rb
@@ -6,6 +6,7 @@
require 'rspec/rails/example/mailer_example_group'
require 'rspec/rails/example/routing_example_group'
require 'rspec/rails/example/model_example_group'
+require 'rspec/rails/example/feature_example_group'
RSpec::configure do |c|
def c.escaped_path(*parts)
@@ -35,4 +36,7 @@ def c.escaped_path(*parts)
c.include RSpec::Rails::ViewExampleGroup, :type => :view, :example_group => {
:file_path => c.escaped_path(%w[spec views])
}
+ c.include RSpec::Rails::FeatureExampleGroup, :type => :feature, :example_group => {
+ :file_path => c.escaped_path(%w[spec features])
+ }
end
View
12 lib/rspec/rails/example/feature_example_group.rb
@@ -0,0 +1,12 @@
+module RSpec::Rails
+ module FeatureExampleGroup
+ extend ActiveSupport::Concern
+ include RSpec::Rails::RailsExampleGroup
+
+ included do
+ metadata[:type] = :feature
+
+ include Rails.application.routes.url_helpers
+ end
+ end
+end
View
10 lib/rspec/rails/vendor/capybara.rb
@@ -12,9 +12,7 @@
RSpec.configure do |c|
if defined?(Capybara::DSL)
c.include Capybara::DSL, :type => :controller
- c.include Capybara::DSL, :example_group => {
- :file_path => c.escaped_path(%w[spec features])
- }
+ c.include Capybara::DSL, :type => :feature
end
if defined?(Capybara::RSpecMatchers)
@@ -27,11 +25,7 @@
}
end
- if defined?(Capybara::RSpecMatchers) || defined?(Capybara::DSL)
- c.include RSpec::Rails::RailsExampleGroup, :example_group => {
- :file_path => c.escaped_path(%w[spec features])
- }
- else
+ unless defined?(Capybara::RSpecMatchers) || defined?(Capybara::DSL)
c.include Capybara, :type => :request
c.include Capybara, :type => :controller
end
View
26 spec/rspec/rails/example/feature_example_group_spec.rb
@@ -0,0 +1,26 @@
+require "spec_helper"
+
+module RSpec::Rails
+ describe FeatureExampleGroup do
+ it { should be_included_in_files_in('./spec/features/') }
+ it { should be_included_in_files_in('.\\spec\\features\\') }
+
+ it "adds :type => :model to the metadata" do
+ group = RSpec::Core::ExampleGroup.describe do
+ include FeatureExampleGroup
+ end
+ group.metadata[:type].should eq(:feature)
+ end
+
+ it "includes Rails route helpers" do
+ Rails.application.routes.draw do
+ get "/foo", :as => :foo, :to => "foo#bar"
+ end
+
+ group = RSpec::Core::ExampleGroup.describe do
+ include FeatureExampleGroup
+ end
+ group.new.foo_path.should == "/foo"
+ end
+ end
+end
Something went wrong with that request. Please try again.