Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 280 lines (247 sloc) 8.743 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
require "spec_helper"

module RSpec::Core
  describe Hooks do
    class HooksHost
      include Hooks
    end

    [:before, :after, :around].each do |type|
      [:each, :all].each do |scope|
        next if type == :around && scope == :all

        describe "##{type}(#{scope})" do
          it_behaves_like "metadata hash builder" do
            define_method :metadata_hash do |*args|
              instance = HooksHost.new
              args.unshift scope if scope
              hooks = instance.send(type, *args) {}
              hooks.first.options
            end
          end
        end
      end

      [true, false].each do |config_value|
        context "when RSpec.configuration.treat_symbols_as_metadata_keys_with_true_values is set to #{config_value}" do
          before(:each) do
            Kernel.stub(:warn)
            RSpec.configure { |c| c.treat_symbols_as_metadata_keys_with_true_values = config_value }
          end

          describe "##{type}(no scope)" do
            let(:instance) { HooksHost.new }

            it "defaults to :each scope if no arguments are given" do
              hooks = instance.send(type) {}
              hook = hooks.first
              instance.hooks[type][:each].should include(hook)
            end

            it "defaults to :each scope if the only argument is a metadata hash" do
              hooks = instance.send(type, :foo => :bar) {}
              hook = hooks.first
              instance.hooks[type][:each].should include(hook)
            end

            it "raises an error if only metadata symbols are given as arguments" do
              expect { instance.send(type, :foo, :bar) {} }.to raise_error(ArgumentError)
            end
          end
        end
      end
    end

    [:before, :after].each do |type|
      [:each, :all, :suite].each do |scope|
        [true, false].each do |config_value|
          context "when RSpec.configuration.treat_symbols_as_metadata_keys_with_true_values is set to #{config_value}" do
            before(:each) do
              RSpec.configure { |c| c.treat_symbols_as_metadata_keys_with_true_values = config_value }
            end

            describe "##{type}(#{scope.inspect})" do
              let(:instance) { HooksHost.new }
              let!(:hook) do
                hooks = instance.send(type, scope) {}
                hooks.first
              end

              it "does not make #{scope.inspect} a metadata key" do
                hook.options.should be_empty
              end

              it "is scoped to #{scope.inspect}" do
                instance.hooks[type][scope].should include(hook)
              end
            end
          end
        end
      end
    end

    describe "#around" do
      context "when not running the example within the around block" do
        it "does not run the example" do
          examples = []
          group = ExampleGroup.describe do
            around do |example|
            end
            it "foo" do
              examples << self
            end
          end
          group.run
          examples.should have(0).example
        end
      end

      context "when running the example within the around block" do
        it "runs the example" do
          examples = []
          group = ExampleGroup.describe do
            around do |example|
              example.run
            end
            it "foo" do
              examples << self
            end
          end
          group.run
          examples.should have(1).example
        end
      end

      context "when running the example within a block passed to a method" do
        it "runs the example" do
          examples = []
          group = ExampleGroup.describe do
            def yielder
              yield
            end

            around do |example|
              yielder { example.run }
            end
            it "foo" do
              examples << self
            end
          end
          group.run
          examples.should have(1).example
        end
      end

      describe Hooks::Hook do
        it "requires a block" do
          lambda {
            Hooks::BeforeHook.new :foo => :bar
          }.should raise_error("no block given for before hook")
        end
      end
    end

    describe "prepend_before" do
      it "should prepend before callbacks so they are run prior to other before filters for the specified scope" do
        fiddle = []
        example_group = ExampleGroup.describe do
          around do |example|
            example.run
          end
          it "foo" do
            examples << self
          end
        end
        example_group.prepend_before(:all) { fiddle << "prepend_before(:all)" }
        example_group.before(:all) { fiddle << "before(:all)" }
        example_group.prepend_before(:each) { fiddle << "prepend_before(:each)" }
        example_group.before(:each) { fiddle << "before(:each)" }
        RSpec.configure { |config| config.prepend_before(:each) { fiddle << "config.prepend_before(:each)" } }
        RSpec.configure { |config| config.prepend_before(:all) { fiddle << "config.prepend_before(:all)" } }
        example_group.run
        fiddle.should == [
            'config.prepend_before(:all)',
            'prepend_before(:all)',
            'before(:all)',
            "config.prepend_before(:each)",
            'prepend_before(:each)',
            'before(:each)'
        ]
      end
    end

    describe "#append_before" do

      it "should order before callbacks from global to local" do
        order = []
        example_group = ExampleGroup.describe do
          around do |example|
            example.run
          end
          it "foo" do
            examples << self
          end
        end
        example_group.append_before(:each) do
          order << :example_group_append_before_each
        end
        RSpec.configure { |config| config.append_before { order << :append_before_each } } # default is :each
        RSpec.configure { |config| config.append_before(:all) { order << :append_before_all } }
        RSpec.configure { |config| config.before(:all) { order << :before_all } }

        example_group.append_before(:all) do
          order << :example_group_append_before_all
        end

        example_group.run
        order.should == [
            :append_before_all,
            :before_all,
            :example_group_append_before_all,
            :append_before_each,
            :example_group_append_before_each,
        ]
      end
    end

    describe "#prepend_after" do

      it "should order after callbacks from global to local" do
        order = []
        example_group = ExampleGroup.describe do
          around do |example|
            example.run
          end
          it "foo" do
            examples << self
          end
        end

        RSpec.configure { |config| config.prepend_after(:all) { order << :prepend__after_all } }
        RSpec.configure { |config| config.prepend_after(:each) { order << :prepend__after_each } }
        example_group.prepend_after(:all) do
          order << :example_group_prepend_after_all
        end
        example_group.prepend_after(:each) do
          order << :example_group_prepend_after_each
        end
        example_group.run
        order.should == [
            :example_group_prepend_after_each,
            :prepend__after_each,
            :example_group_prepend_after_all,
            :prepend__after_all
        ]
      end
    end

    describe "#append_after" do

      it "should append callbacks so they are run after other after filters for the specified scope" do
        order = []
        example_group = ExampleGroup.describe do
          around do |example|
            example.run
          end
          it "foo" do
            examples << self
          end
        end

        RSpec.configure { |config| config.append_after(:all) { order << :append__after_all } }
        RSpec.configure { |config| config.after(:all) { order << :after_all } }
        example_group.append_after(:all) do
          order << :example_group_append__after_all
        end

        RSpec.configure { |config| config.append_after(:each) { order << :append_after_each } }

        example_group.append_after(:each) do
          order << :example_group_append__after_each
        end

        example_group.after(:each) do
          order << :example_group__after_each
        end

        example_group.run
        order.should == [
            :example_group__after_each,
            :example_group_append__after_each,
            :append_after_each,
            :example_group_append__after_all,
            :after_all,
            :append__after_all
        ]
      end

    end
  end
end
Something went wrong with that request. Please try again.