Skip to content

Commit

Permalink
Add hooks and use them in capybara
Browse files Browse the repository at this point in the history
  • Loading branch information
josepjaume committed Oct 2, 2011
1 parent 5a0a3b6 commit 6244a6a
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 9 deletions.
4 changes: 1 addition & 3 deletions lib/spinach/capybara.rb
Expand Up @@ -21,7 +21,7 @@ def self.included(base)
include ::Capybara::DSL
include InstanceMethods

def after
after_scenario do
::Capybara.current_session.reset! if ::Capybara.app
end
end
Expand All @@ -31,5 +31,3 @@ module InstanceMethods
end
end
end

Spinach::Feature.send(:include, Spinach::Feature::Capybara)
10 changes: 8 additions & 2 deletions lib/spinach/feature.rb
@@ -1,4 +1,5 @@
require 'minitest/spec'
require 'hooks'
MiniTest::Spec.new nil

module Spinach
Expand All @@ -7,9 +8,14 @@ module Spinach
class Feature
include DSL
include MiniTest::Assertions
include Hooks

def before; end;
def after; end;
define_hook :before
define_hook :after
define_hook :before_scenario
define_hook :after_scenario
define_hook :before_step
define_hook :after_step

def self.inherited(base)
Spinach.features << base
Expand Down
4 changes: 2 additions & 2 deletions lib/spinach/runner/feature.rb
Expand Up @@ -53,14 +53,14 @@ def scenarios
def run
reporter.feature(feature_name)
failures = []
feature.run_hook :before, feature_name
scenarios.each do |scenario|
if !@scenario_line || scenario['line'].to_s == @scenario_line
feature.send(:before)
failure = Scenario.new(feature_name, feature, scenario, reporter).run
failures << failure if failure
feature.send(:after)
end
end
feature.run_hook :after, feature_name

unless failures.length.zero?
reporter.error_summary(failures)
Expand Down
4 changes: 4 additions & 0 deletions lib/spinach/runner/scenario.rb
Expand Up @@ -26,10 +26,12 @@ def initialize(feature_name, feature, data, reporter)
#
def run
reporter.scenario(name)
feature.run_hook :before_scenario, name
steps.each do |step|
keyword = step['keyword'].strip
name = step['name'].strip
line = step['line']
feature.run_hook :before_step, keyword, name
unless @failure
begin
feature.execute_step(name)
Expand All @@ -47,7 +49,9 @@ def run
else
reporter.step(keyword, name, :skip)
end
feature.run_hook :after_step, keyword, name
end
feature.run_hook :after_scenario, name
@failure
end
end
Expand Down
1 change: 1 addition & 0 deletions spinach.gemspec
Expand Up @@ -12,6 +12,7 @@ Gem::Specification.new do |gem|
gem.add_runtime_dependency 'gherkin'
gem.add_runtime_dependency 'minitest'
gem.add_runtime_dependency 'colorize'
gem.add_runtime_dependency 'hooks'
gem.add_development_dependency 'purdytest'
gem.add_development_dependency 'rake'
gem.add_development_dependency 'mocha'
Expand Down
21 changes: 21 additions & 0 deletions test/spinach/capybara_test.rb
Expand Up @@ -11,6 +11,11 @@
end
Capybara.app = @sinatra_app
@feature = Class.new(Spinach::Feature) do
include Spinach::Feature::Capybara
Given "Hello" do
end
Then "Goodbye" do
end
def go_home
visit "/"
page
Expand All @@ -24,4 +29,20 @@ def go_home
page = @feature.go_home
page.has_content?('Hello world').must_equal true
end
it "resets the capybara session after each scenario" do
@feature_runner = Spinach::Runner::Feature.new(
stub_everything, stub_everything)
Spinach::Parser.any_instance.stubs(content: "
Feature: A test feature
Scenario: A test scenario
Given Hello
Then Goodbye
Scenario: Another test scenario
Given Hello
Then Goodbye
").at_least_once
@feature_runner.stubs(feature: @feature).at_least_once
Capybara.current_session.expects(:reset!).twice
@feature_runner.run
end
end
4 changes: 2 additions & 2 deletions test/spinach/runner/feature_test.rb
Expand Up @@ -69,14 +69,14 @@

it 'calls the steps as expected' do
seq = sequence('feature')
@feature.feature.expects(:run_hook).with(:before, "A cool feature")
3.times do
@feature.feature.expects(:before).in_sequence(seq)
Spinach::Runner::Scenario.
expects(:new).
returns(stub_everything).
in_sequence(seq)
@feature.feature.expects(:after).in_sequence(seq)
end
@feature.feature.expects(:run_hook).with(:after, "A cool feature")
@feature.run
end

Expand Down
20 changes: 20 additions & 0 deletions test/spinach/runner/scenario_test.rb
Expand Up @@ -73,5 +73,25 @@
@scenario.run.must_equal nil
end

describe "hooks" do
it "fires up the scenario hooks" do
@feature.expects(:execute_step).raises(Spinach::StepNotDefinedException.new('foo', 'bar'))
@feature.expects(:run_hook).with(:before_scenario, "A cool scenario")
@feature.expects(:run_hook).with(:after_scenario, "A cool scenario")
@scenario.run
end
it "fires up the step hooks" do
@feature.expects(:execute_step).raises(Spinach::StepNotDefinedException.new('foo', 'bar'))
%w{before_step after_step}.each do |hook|
@feature.expects(:run_hook).with(
hook.to_sym, "Given", "I herd you like steps")
@feature.expects(:run_hook).with(
hook.to_sym, "When", "I test steps")
@feature.expects(:run_hook).with(
hook.to_sym, "Then", "I go step by step")
end
@scenario.run
end
end
end
end

0 comments on commit 6244a6a

Please sign in to comment.