💨
使えるRSpec入門とBetter SpecsでRSpecの書き方を学びました
使えるRSpec入門とBetter SpecsでRSpecの書き方を学びました。今後、次の決まりで書いていきます。
- it、specify、exampleの使い分けをせず常にitを使う
- not_to、to_notの使い分けをせず常にnot_toを使う
- xxx?ではなく、常にbe_xxxを使う
練習
Object#in?
を自作実装して、練習します。
# frozen_string_literal: true
require 'rspec'
RSpec.describe Object do
class Object
def in?(another_object)
raise ArgumentError unless respond_to?(:include?)
another_object.include?(self)
end
end
describe '#in?' do
describe 'include?メソッドが定義されている場合' do
let(:names) { %w[木村 王子 天道虫] }
context '配列に該当文字列が含まれる場合' do
let(:name) { '木村' }
it { expect(name).to be_in(names) }
end
context '配列に該当文字列が含まれない場合' do
let(:name) { '蜜柑' }
it { expect(name).not_to be_in(names) }
end
end
describe 'include?メソッドが定義されていない場合' do
class Klass; end
let(:klass_array) { [Klass.new] }
let(:klass) { Klass.new }
it { expect { klass.in?(klass_array) }.to raise_error(ArgumentError) }
end
end
end
Discussion