From 2c3f1d0780a09f6491230696de6b947681060947 Mon Sep 17 00:00:00 2001 From: Josep Jaume Date: Fri, 14 Oct 2011 01:13:40 +0200 Subject: [PATCH] End refactor - missing docs --- features/support/spinach_runner.rb | 11 +- lib/spinach/generators.rb | 2 +- lib/spinach/hooks.rb | 2 +- lib/spinach/reporter.rb | 52 ++++--- lib/spinach/runner.rb | 4 +- lib/spinach/runner/feature_runner.rb | 4 +- lib/spinach/runner/scenario_runner.rb | 3 +- test/spinach/capybara_test.rb | 3 +- test/spinach/generators_test.rb | 4 +- test/spinach/reporter_test.rb | 161 +++++++------------- test/spinach/runner/scenario_runner_test.rb | 2 - 11 files changed, 100 insertions(+), 148 deletions(-) diff --git a/features/support/spinach_runner.rb b/features/support/spinach_runner.rb index bad25887..3aca46a9 100644 --- a/features/support/spinach_runner.rb +++ b/features/support/spinach_runner.rb @@ -5,15 +5,8 @@ module SpinachRunner include Aruba::Api def self.included(base) - base.class_eval do - before_scenario do - in_current_dir do - FileUtils.rm_rf("features") - end - end - before_scenario do - @aruba_timeout_seconds = 6 - end + Spinach.hooks.before_scenario do + @aruba_timeout_seconds = 6 end end diff --git a/lib/spinach/generators.rb b/lib/spinach/generators.rb index 6761b8c6..ca51dce0 100644 --- a/lib/spinach/generators.rb +++ b/lib/spinach/generators.rb @@ -5,7 +5,7 @@ module Spinach module Generators # Binds the feature generator to the "feature not found" hook def self.bind - Spinach::Runner::FeatureRunner.when_not_found do |data| + Spinach.hooks.on_undefined_feature do |data| Spinach::Generators.generate_feature(data) end end diff --git a/lib/spinach/hooks.rb b/lib/spinach/hooks.rb index 96507a90..553683a5 100644 --- a/lib/spinach/hooks.rb +++ b/lib/spinach/hooks.rb @@ -35,7 +35,7 @@ def reset def run_hook(name, *args) if callbacks = @hooks[name.to_sym] - callbacks.each{ |c| c.call(args) } + callbacks.each{ |c| c.call(*args) } end end diff --git a/lib/spinach/reporter.rb b/lib/spinach/reporter.rb index 3a072642..a2f7ecc2 100644 --- a/lib/spinach/reporter.rb +++ b/lib/spinach/reporter.rb @@ -19,36 +19,32 @@ def initialize(options = {}) # A Hash with options for the reporter # - attr_reader :options - - attr_accessor :current_feature, :current_scenario + attr_reader :options, :current_feature, :current_scenario attr_reader :undefined_steps, :failed_steps, :error_steps, :undefined_features, :successful_steps # Hooks the reporter to the runner endpoints def bind - runner.after_run method(:after_run) - feature_runner.before_run method(:before_feature_run) - feature_runner.after_run method(:after_feature_run) - feature_runner.when_not_found method(:on_feature_not_found) - scenario_runner.before_run method(:before_scenario_run) - scenario_runner.after_run method(:after_scenario_run) - scenario_runner.on_successful_step method(:on_successful_step) - scenario_runner.on_undefined_step method(:on_undefined_step) - scenario_runner.on_failed_step method(:on_failed_step) - scenario_runner.on_error_step method(:on_error_step) - scenario_runner.on_skipped_step method(:on_skipped_step) + Spinach.hooks.tap do |hooks| + hooks.after_run { |*args| after_run(*args) } + hooks.before_feature { |*args| before_feature_run(*args) } + hooks.after_feature { |*args| after_feature_run(*args) } + hooks.on_undefined_feature { |*args| on_feature_not_found(*args) } + hooks.before_scenario { |*args| before_scenario_run(*args) } + hooks.after_scenario { |*args| after_scenario_run(*args) } + hooks.on_successful_step { |*args| on_successful_step(*args) } + hooks.on_undefined_step { |*args| on_undefined_step(*args) } + hooks.on_failed_step { |*args| on_failed_step(*args) } + hooks.on_error_step { |*args| on_error_step(*args) } + hooks.on_skipped_step { |*args| on_skipped_step(*args) } - feature_runner.before_run method(:current_feature=) - feature_runner.after_run method(:clear_current_feature) - scenario_runner.before_run method(:current_scenario=) - scenario_runner.after_run method(:clear_current_scenario) + hooks.before_feature { |*args| set_current_feature(*args) } + hooks.after_feature { |*args| clear_current_feature(*args) } + hooks.before_scenario { |*args| set_current_scenario(*args) } + hooks.after_scenario { |*args| clear_current_scenario(*args) } + end end - def feature_runner; Runner::FeatureRunner; end - def scenario_runner; Runner::ScenarioRunner; end - def runner; Runner; end - def after_run(*args); end; def before_feature_run(*args); end def after_feature_run(*args); end @@ -61,12 +57,20 @@ def on_error_step(*args); end; def on_undefined_step(*args); end; def on_skipped_step(*args); end; + def set_current_feature(data) + @current_feature = data + end + def clear_current_feature(*args) - self.current_feature = nil + @current_feature = nil + end + + def set_current_scenario(data) + @current_scenario = data end def clear_current_scenario(*args) - self.current_scenario = nil + @current_scenario = nil end end diff --git a/lib/spinach/runner.rb b/lib/spinach/runner.rb index 0d23e263..0a0926f2 100644 --- a/lib/spinach/runner.rb +++ b/lib/spinach/runner.rb @@ -56,7 +56,7 @@ def run require_dependencies require_suites - run_hook :before_run + Spinach.hooks.run_before_run successful = true @@ -65,7 +65,7 @@ def run successful = false unless success end - run_hook :after_run, successful + Spinach.hooks.run_after_run(successful) successful end diff --git a/lib/spinach/runner/feature_runner.rb b/lib/spinach/runner/feature_runner.rb index 8599ab3f..f95b8322 100644 --- a/lib/spinach/runner/feature_runner.rb +++ b/lib/spinach/runner/feature_runner.rb @@ -52,7 +52,7 @@ def scenarios # # @api public def run - Spinach.hooks.run_before_run data + Spinach.hooks.run_before_feature data scenarios.each do |scenario| if !@scenario_line || scenario['line'].to_s == @scenario_line @@ -65,7 +65,7 @@ def run Spinach.hooks.run_on_undefined_feature data, e @failed = true ensure - Spinach.hooks.run_after_run data + Spinach.hooks.run_after_feature data return !@failed end end diff --git a/lib/spinach/runner/scenario_runner.rb b/lib/spinach/runner/scenario_runner.rb index 2ec732b3..832c9be0 100644 --- a/lib/spinach/runner/scenario_runner.rb +++ b/lib/spinach/runner/scenario_runner.rb @@ -34,13 +34,14 @@ def feature_steps # true if this scenario succeeded, false if not def run Spinach.hooks.run_before_scenario data - feature_steps.run_hook :before_scenario, data steps.each do |step| Spinach.hooks.run_before_step step unless @exception begin step_location = feature_steps.execute_step(step['name']) Spinach.hooks.run_on_successful_step step, step_location + rescue Spinach::FeatureStepsNotFoundException => e + raise e rescue *Spinach.config[:failure_exceptions] => e @exception = e Spinach.hooks.run_on_failed_step step, @exception, step_location diff --git a/test/spinach/capybara_test.rb b/test/spinach/capybara_test.rb index d1530428..de2a9603 100644 --- a/test/spinach/capybara_test.rb +++ b/test/spinach/capybara_test.rb @@ -1,4 +1,5 @@ require 'test_helper' +require 'spinach' require 'spinach/capybara' require 'sinatra' @@ -50,7 +51,7 @@ def go_home Spinach::Runner::ScenarioRunner.any_instance.stubs(feature_steps: @feature) - Capybara.current_session.expects(:reset!).times(3) + Capybara.current_session.expects(:reset!).at_least_once @feature_runner.run end diff --git a/test/spinach/generators_test.rb b/test/spinach/generators_test.rb index 9a831a24..b9d09f9c 100644 --- a/test/spinach/generators_test.rb +++ b/test/spinach/generators_test.rb @@ -22,8 +22,8 @@ it "binds the generator to the missing feature hook" do subject.expects(:generate_feature).with(data) subject.bind - Spinach::Runner::FeatureRunner.new(stub_everything).run_hook :when_not_found, data - Spinach::Runner::FeatureRunner._when_not_found_callbacks = [] + Spinach.hooks.run_on_undefined_feature data + Spinach.hooks.reset end end diff --git a/test/spinach/reporter_test.rb b/test/spinach/reporter_test.rb index 7afd250f..774a4148 100644 --- a/test/spinach/reporter_test.rb +++ b/test/spinach/reporter_test.rb @@ -10,11 +10,6 @@ module Spinach @reporter = Reporter.new(@options) end - describe "#initialize" do - it "initializes the option hash" do - end - end - describe "#options" do it "returns the options passed to the reporter" do @reporter.options[:backtrace].must_equal true @@ -52,124 +47,84 @@ module Spinach @reporter.stubs(:method) end - it "binds a callback after running all the suite" do - @reporter.expects(:method).with(:after_run).returns(@callback) - @reporter.runner.expects(:after_run).with(@callback) - @reporter.bind - end - - it "binds a callback before running every feature" do - @reporter.expects(:method).with(:before_feature_run).returns(@callback) - @reporter.feature_runner.expects(:before_run).with(@callback) - @reporter.bind - end - - it "binds a callback after running every feature" do - @reporter.expects(:method).with(:after_feature_run).returns(@callback) - @reporter.feature_runner.expects(:after_run).with(@callback) - @reporter.bind - end - - it "binds a callback for not defined features" do - @reporter.expects(:method).with(:on_feature_not_found).returns(@callback) - @reporter.feature_runner.expects(:when_not_found).with(@callback) - @reporter.bind - end - - it "binds a callback before running every scenario" do - @reporter.expects(:method).with(:before_scenario_run).returns(@callback) - @reporter.scenario_runner.expects(:before_run).with(@callback) - @reporter.bind - end + describe "bindings" do + before do + @reporter.bind + end - it "binds a callback after running every feature" do - @reporter.expects(:method).with(:after_scenario_run).returns(@callback) - @reporter.scenario_runner.expects(:after_run).with(@callback) - @reporter.bind - end + after do + Spinach.hooks.reset + end - describe "when running steps" do - %w{successful failed error undefined skipped}.each do |type| - it "binds a callback after running a #{type} step" do - @reporter.expects(:method).with(:"on_#{type}_step").returns(@callback) - @reporter.scenario_runner.expects(:"on_#{type}_step").with(@callback) - @reporter.bind - end + it "binds a callback after running" do + @reporter.expects(:after_run) + Spinach.hooks.run_after_run end - end - describe "binds the context methods" do - it "binds the current feature setter" do - @reporter.expects(:method).with(:current_feature=).returns(@callback) - @reporter.feature_runner.expects(:before_run).with(@callback) - @reporter.bind + it "binds a callback before running every feature" do + @reporter.expects(:before_feature_run) + Spinach.hooks.run_before_feature(anything) end - it "binds the current feature clearer" do - @reporter.expects(:method).with(:clear_current_feature).returns(@callback) - @reporter.feature_runner.expects(:after_run).with(@callback) - @reporter.bind + it "binds a callback after running every feature" do + @reporter.expects(:after_feature_run) + Spinach.hooks.run_after_feature end - it "binds the current scenario setter" do - @reporter.expects(:method).with(:current_scenario=).returns(@callback) - @reporter.scenario_runner.expects(:before_run).with(@callback) - @reporter.bind + it "binds a callback when a feature is not found" do + @reporter.expects(:on_feature_not_found) + Spinach.hooks.run_on_undefined_feature end - it "binds the current feature clearer" do - @reporter.expects(:method).with(:clear_current_scenario).returns(@callback) - @reporter.scenario_runner.expects(:after_run).with(@callback) - @reporter.bind + it "binds a callback before every scenario" do + @reporter.expects(:before_scenario_run) + Spinach.hooks.run_before_scenario(anything) end - end - end - describe "#clear_current_feature" do - it "clears the current feature" do - @reporter.current_feature = mock - @reporter.clear_current_feature - @reporter.current_feature.must_equal nil - end - end + it "binds a callback after every scenario" do + @reporter.expects(:after_scenario_run) + Spinach.hooks.run_after_scenario + end - describe "#clear_current_scenario" do - it "clears the current scenario" do - @reporter.current_scenario = mock - @reporter.clear_current_scenario - @reporter.current_scenario.must_equal nil - end - end + it "binds a callback after every successful step" do + @reporter.expects(:on_successful_step) + Spinach.hooks.run_on_successful_step + end - describe "runner classes" do - describe "#feature_runner" do - it "returns a runner class" do - @reporter.feature_runner.must_be_kind_of Class + it "binds a callback after every failed step" do + @reporter.expects(:on_failed_step) + Spinach.hooks.run_on_failed_step end - end - describe "#scenario_runner" do - it "returns a runner class" do - @reporter.scenario_runner.must_be_kind_of Class + it "binds a callback after every error step" do + @reporter.expects(:on_error_step) + Spinach.hooks.run_on_error_step end - end - describe "#runner" do - it "returns a runner class" do - @reporter.runner.must_be_kind_of Class + it "binds a callback after every skipped step" do + @reporter.expects(:on_skipped_step) + Spinach.hooks.run_on_skipped_step end - end - end - describe "callback methods" do - %w{ - after_run before_feature_run after_feature_run before_scenario_run - after_scenario_run on_successful_step on_failed_step on_error_step - on_undefined_step on_skipped_step - }.each do |callback| - describe "##{callback}" do - it "does nothing" do - @reporter.send(callback) + describe "internals" do + it "binds a callback before running" do + @reporter.expects(:set_current_feature) + Spinach.hooks.run_before_feature({}) + end + + it "binds a callback after running" do + @reporter.expects(:clear_current_feature) + Spinach.hooks.run_after_feature + end + + it "binds a callback before every scenario" do + @reporter.expects(:set_current_scenario) + Spinach.hooks.run_before_scenario + end + + it "binds a callback after every scenario" do + @reporter.expects(:clear_current_scenario) + Spinach.hooks.run_after_scenario end end end diff --git a/test/spinach/runner/scenario_runner_test.rb b/test/spinach/runner/scenario_runner_test.rb index a88ff99f..709a905e 100644 --- a/test/spinach/runner/scenario_runner_test.rb +++ b/test/spinach/runner/scenario_runner_test.rb @@ -63,8 +63,6 @@ Spinach::StepNotDefinedException.new('bar')) Spinach.hooks.expects("run_before_scenario").with(has_value("A cool scenario")) Spinach.hooks.expects("run_after_scenario").with(has_value("A cool scenario")) - Spinach.hooks.expects("run_on_undefined_step").with( - anything, anything, anything) Spinach.hooks.expects("run_on_undefined_step").with( anything, anything, anything) Spinach.hooks.expects("run_on_skipped_step").with(