Skip to content

Commit

Permalink
Set metadata type attribute to :acceptance so that helper modules and…
Browse files Browse the repository at this point in the history
… hooks can be set exclusively for acceptance specs without contaminating other specs in the suite. Closes #12
  • Loading branch information
cavalle committed Jul 17, 2010
1 parent 06ae0d4 commit 5d3a1e9
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 17 deletions.
18 changes: 18 additions & 0 deletions README.rdoc
Expand Up @@ -139,6 +139,24 @@ You run your acceptance specs just like your regular specs. Individually...
...you can also do:

$ rake spec:acceptance

== RSpec & Metadata

Steak scenarios are just regular RSpec examples with their metadata attribute
`:type` set to `:acceptance`. That's useful if you want to make sure you
include helpers or set hooks only for your acceptance specs and not for the
rest of the specs in your suite.

You'd do it this way:

RSpec.configure do |config|
# include MyHelpers module in every acceptance spec
config.include MyHelpers, :type => :acceptance

config.before(:each, :type => :acceptance) do
# Some code to run before any acceptance spec
end
end

== Resources

Expand Down
18 changes: 8 additions & 10 deletions lib/generators/steak/templates/acceptance_helper.rb
Expand Up @@ -7,24 +7,22 @@
config.mode = :rack
end
module AppHelper
module Steak::Webrat
include Rack::Test::Methods
include Webrat::Methods
include Webrat::Matchers
def app
Rails.application
end
end
RSpec.configure do |config|
config.include Rack::Test::Methods
config.include Webrat::Methods
config.include Webrat::Matchers
config.include AppHelper
end
RSpec.configuration.include Steak::Webrat, :type => :acceptance
<%- else -%>
require 'capybara/rails'
RSpec.configure do |config|
config.include Capybara
end
RSpec.configuration.include Capybara, :type => :acceptance
<%- end -%>
# Put your acceptance spec helpers inside /spec/acceptance/support
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/steak/templates/helpers.rb
Expand Up @@ -2,4 +2,4 @@ module HelperMethods
# Put helper methods you need to be available in all tests here.
end

RSpec.configuration.include(HelperMethods), :example_group => { :file_path => %r{\bspec/acceptance/} }
RSpec.configuration.include HelperMethods, :type => :acceptance
2 changes: 1 addition & 1 deletion lib/generators/steak/templates/paths.rb
Expand Up @@ -6,4 +6,4 @@ def homepage
end
end

RSpec.configuration.include(NavigationHelpers), :example_group => { :file_path => %r{\bspec/acceptance/} }
RSpec.configuration.include NavigationHelpers, :type => :acceptance
20 changes: 15 additions & 5 deletions lib/steak.rb
@@ -1,12 +1,22 @@
require 'rspec/core'

class RSpec::Core::ExampleGroup
class << self
alias scenario example
alias background before
module Steak
module AcceptanceExampleGroup
def self.included(base)
base.instance_eval do
alias scenario example
alias background before
end
end
end
end

module RSpec::Core::ObjectExtensions
alias feature describe
def feature(*args, &block)
args << {} unless args.last.is_a?(Hash)
args.last.update :type => :acceptance, :steak => true
describe(*args, &block)
end
end

RSpec.configuration.include Steak::AcceptanceExampleGroup, :type => :acceptance
28 changes: 28 additions & 0 deletions spec/acceptance/basic_spec.rb
Expand Up @@ -47,4 +47,32 @@
output = run_spec spec_file
output.should =~ /1 example, 0 failures/
end

scenario "Acceptance spec metadata" do
spec_file = create_spec <<-SPEC
require '#{File.dirname(__FILE__) + "/../../lib/steak"}'
feature "Minimal spec" do
scenario "should have acceptance metadata" do
example.metadata[:type].should == :acceptance
example.metadata[:steak].should be_true
end
end
SPEC
output = run_spec spec_file
output.should =~ /1 example, 0 failures/
end

scenario "Regular specs doesn't have acceptance metadata" do
spec_file = create_spec <<-SPEC
require '#{File.dirname(__FILE__) + "/../../lib/steak"}'
describe "Regular spec" do
it "should not have acceptance metadata" do
example.metadata[:type].should_not == :acceptance
example.metadata[:steak].should_not be_true
end
end
SPEC
output = run_spec spec_file
output.should =~ /1 example, 0 failures/
end
end
15 changes: 15 additions & 0 deletions spec/acceptance/rails_spec.rb
Expand Up @@ -38,6 +38,21 @@
output.should =~ /1 example, 1 failure/
end

scenario "Path helpers are available" do
rails_app = create_rails_app
spec_file = create_spec :path => rails_app + "/spec/acceptance",
:content => <<-SPEC
require File.dirname(__FILE__) + "/acceptance_helper.rb"
feature "Minimal spec" do
scenario "First scenario" do
homepage.should == "/"
end
end
SPEC
output = run_spec spec_file, File.join(File.dirname(spec_file), '../..')
output.should =~ /1 example, 0 failures/
end

scenario "Running rake stats" do
rails_app = create_rails_app

Expand Down

0 comments on commit 5d3a1e9

Please sign in to comment.