PerlerのRuby日記

Rubyとか

RSpecのbeforeはeachよりallが先に動く

はまったのでメモ。

beforeがどこに書かれているかに関わらず、eachよりもallの方が先に動くらしい。


spec_helper.rb

require "rspec"

RSpec.configure do |config|
  config.before(:each) do
    puts "config before each"
  end
end

shared_context "sharecon" do
  before(:all) do
    puts "sharecon before all"
  end
end

test_spec.rb

require "spec_helper"

describe "test" do
  context "test" do
    include_context "sharecon"
    it {
      1.should == 1
    }
  end
end
sharecon before all
config before each
.

Finished in 0.00031 seconds
1 example, 0 failures

config.before(:each)の中でTimecop使ってfreezeしてたのに、

shared_contextの中で作ったDBのcreated_atがテストを動かした時間になってて、なんでかなーって調べてわかった。