Skip to content

Instantly share code, notes, and snippets.

@ukstudio
Forked from yuya-takeyama/marker_spec_01.rb
Created June 27, 2011 03:06
Show Gist options
  • Save ukstudio/1048253 to your computer and use it in GitHub Desktop.
Save ukstudio/1048253 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
@twada
Copy link

twada commented Jun 27, 2011

xUnit Test Patterns に Testcase class per Feature と Testcase class per Fixture というパターンがあって、 per Feature がメソッド単位、 per Fixture がコンテクスト単位に合致すると考えています。

@yuya-takeyama
Copy link

やっぱりちゃんと名前が付いているんですね.
該当箇所を読み返してみます.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment