Rails5 で rspec の基礎文法を試す
Ruby Ruby on Rails
Published: 2019-05-26

やったこと

rspec の基礎文法について調べました。

確認環境

$ ruby --version
ruby 2.6.2p47 (2019-03-13 revision 67232) [x86_64-darwin17]

$ rails --version
Rails 5.2.3

$ gem list | grep rspec-rails
rspec-rails (3.8.2)

feature test

spec/features/session_spec.rb

require 'rails_helper'

RSpec.feature 'session management', :type => :feature do

  scenario 'scenario test' do

  end
end

出力結果

$ bundle exec rspec -f d spec/features/session_spec.rb

session management
  scenario test

Finished in 0.00364 seconds (files took 1.08 seconds to load)
1 example, 0 failures

model test

spec/models/task2_spec.rb

require 'rails_helper'

RSpec.describe Task2, type: :model do
  context 'is context1' do
    it 'is one thing' do
    end
  end

  describe 'is context2' do
    specify 'is one thing in specify' do
    end

    example 'is one thing in example' do
    end
  end
end
  • context, describe は ExampleGroup を作成します
  • it, specify, example は Example を作成します

出力結果

$ bundle exec rspec -f d spec/models/task2_spec.rb

Task2
  is context1
    is one thing
  is context2
    is one thing in specify
    is one thing in example

Finished in 0.0125 seconds (files took 1.02 seconds to load)
3 examples, 0 failures

参考