10
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RSpecのConfig#before(:all)メモ

Last updated at Posted at 2013-11-10

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
10
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?