Skip to content

Instantly share code, notes, and snippets.

@yuya-takeyama
Created June 26, 2011 18:40
Show Gist options
  • Save yuya-takeyama/1047848 to your computer and use it in GitHub Desktop.
Save yuya-takeyama/1047848 to your computer and use it in GitHub Desktop.
RSpec の書き方, メソッド単位に describe で分割するパターンと, オブジェクトの状態単位に context で分割するパターンとあると思う.
# メソッド単位に describe で分割するパターン
require 'spec_helper'
module Codebreaker
describe Marker do
let(:secret) { '1234' }
describe '#exact_match_count' do
subject { Marker.new(secret, guess).exact_match_count }
context 'no matches' do
let(:guess) { '5555' }
it { should == 0 }
end
context '4 exact matches' do
let(:guess) { '1234' }
it { should == 4 }
end
context '4 number matches' do
let(:guess) { '2341' }
it { should == 0 }
end
end
describe '#number_match_count' do
subject { Marker.new(secret, guess).number_match_count }
context 'no matches' do
let(:guess) { '5555' }
it { should == 0 }
end
context '4 exact matches do' do
let(:guess) { '1234' }
it { should == 0 }
end
context '4 number matches' do
let(:guess) { '4321' }
it { should == 4 }
end
end
end
end
# オブジェクトの状態単位に context で分割するパターン
require 'spec_helper'
module Codebreaker
describe Marker do
context 'has "1234" as the secret' do
let(:secret) { '1234' }
subject { Marker.new(secret, guess) }
context 'and has "5555" as the guess' do
let(:guess) { '5555' }
its(:mark) { should == '' }
its(:exact_match_count) { should == 0 }
its(:number_match_count) { should == 0 }
end
context 'and has "1234" as the guess' do
let(:guess) { '1234' }
its(:mark) { should == '++++' }
its(:exact_match_count) { should == 4 }
its(:number_match_count) { should == 0 }
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment