Module: Turnip::RSpec

Defined in:
lib/turnip/rspec.rb

Defined Under Namespace

Modules: Execute, Loader

Class Method Summary collapse

Class Method Details

.fetch_current_example(context) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/turnip/rspec.rb', line 71

def fetch_current_example(context)
  if ::RSpec.respond_to?(:current_example)
    ::RSpec.current_example
  else
    context.example
  end
end

.run(feature_file) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/turnip/rspec.rb', line 79

def run(feature_file)
  Turnip::Builder.build(feature_file).features.each do |feature|
    ::RSpec.describe feature.name, feature. do
      before do
        example = Turnip::RSpec.fetch_current_example(self)
        # This is kind of a hack, but it will make RSpec throw way nicer exceptions
        example.[:file_path] ||= feature_file

        feature.backgrounds.map(&:steps).flatten.each do |step|
          run_step(feature_file, step)
        end
      end
      feature.scenarios.each do |scenario|
        instance_eval <<-EOS, feature_file, scenario.line
          describe scenario.name, scenario.metadata_hash do it(scenario.steps.map(&:to_s).join(' -> ')) do
              scenario.steps.each do |step|
                run_step(feature_file, step)
              end
            end
          end
        EOS
      end
    end
  end
end