Rspec勉強中。
テスト本体をこんな感じで書いてみます。
a_spec.rb
require 'spec_helper'
describe 'a spec' do
before(:all) { puts "I'm before(:all) in 1st layer" }
before(:each) { puts "I'm before(:each) in 1st layer" }
describe '1st nested describe' do
before(:all) { puts "I'm before(:all) in 1st nested describe" }
before(:each) { puts "I'm before(:each) in 1st nested describe" }
it { puts "I'm first it in 1st nested describe" }
it { puts "I'm second it in 1st nested describe" }
end
describe '2nd nested describe' do
before(:all) { puts "I'm before(:all) in 2nd nested describe" }
before(:each) { puts "I'm before(:each) in 2nd nested describe" }
it { puts "I'm first it in 2nd nested describe" }
it { puts "I'm second it in 2nd nested describe" }
end
end
そしてHelper。
spec_helper.rb
RSpec.configure do |config|
config.before(:all) {
puts "I'm before(:all) in configuration file."
}
config.before(:each) {
puts "I'm before(:each) in configuration file."
}
end
実行します。
I'm before(:all) on configuration file.
I'm before(:all) in 1st layer
I'm before(:all) in 1st nested describe
I'm before(:each) on configuration file.
I'm before(:each) in 1st layer
I'm before(:each) in 1st nested describe
I'm first it in 1st nested describe
.I'm before(:each) on configuration file.
I'm before(:each) in 1st layer
I'm before(:each) in 1st nested describe
I'm second it in 1st nested describe
.I'm before(:all) in 2nd nested describe
I'm before(:each) on configuration file.
I'm before(:each) in 1st layer
I'm before(:each) in 2nd nested describe
I'm first it in 2nd nested describe
.I'm before(:each) on configuration file.
I'm before(:each) in 1st layer
I'm before(:each) in 2nd nested describe
I'm second it in 2nd nested describe
.
Config#before(:each)は各Specのbefore(:each)のテスト単位と対応してるけど、
Config#before(:all)は実行の単位が対応しないんですねー。
Specのファイル単位で実行されている感じです。
a_spec.rb
require 'spec_helper'
describe 'a spec' do
# config.before(:all) { puts "I'm before(:all) in configuration file." } こんなイメージ
before(:all) { puts "I'm before(:all) in 1st layer" }
before(:each) { puts "I'm before(:each) in 1st layer" }
describe '1st nested describe' do
# ここで実行されるイメージだったけどされない
before(:all) { puts "I'm before(:all) in 1st nested describe" }
before(:each) { puts "I'm before(:each) in 1st nested describe" }
it { puts "I'm first it in 1st nested describe" }
it { puts "I'm second it in 1st nested describe" }
end
describe '2nd nested describe' do
# ここで実行されるイメージだったされない
before(:all) { puts "I'm before(:all) in 2nd nested describe" }
before(:each) { puts "I'm before(:each) in 2nd nested describe" }
it { puts "I'm first it in 2nd nested describe" }
it { puts "I'm second it in 2nd nested describe" }
end
end