-
-
Save ukstudio/1048253 to your computer and use it in GitHub Desktop.
RSpec の書き方, メソッド単位に describe で分割するパターンと, オブジェクトの状態単位に context で分割するパターンとあると思う.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# メソッド単位に 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# オブジェクトの状態単位に 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
xUnit Test Patterns に Testcase class per Feature と Testcase class per Fixture というパターンがあって、 per Feature がメソッド単位、 per Fixture がコンテクスト単位に合致すると考えています。