7. RSpecサンプル修正
context "with 1 exact match duplicated in guess" do
it "returns 0" do
pending("refactor number_match_count")
marker = Marker.new('1234','1155')
marker.number_match_count.should == 0
end
end
最初はpendingにしておくことで
新しい失敗が発生しても混同しなくなる
10. サンプルグループ
サンプルコード
describe "A new Account" do
サブジェクト it "should have a balance of 0" do
account = Account.new
コード
account.balance.should == Money.new(0, :USD)
end
end
12. describe User, "with no roles assigned" do
...
end
=> User with no roles assigned
describe User, "(ロール未割り当て)" do
...
end
=> User (ロール未割り当て)
13. module Authentication
describe User, "with no roles assigned" do
...
end
end
=> Authentication::User with no roles assigned
14. describe User do
describe "with no roles assigned" do
it "is not allowed to view protected content"
...
end
end
end
=>User
with no roles assigned
is not allowed to view protected content
15. サンプルコード
describe Stack do
before(:each) do
@stack = Stack.new
@stack.push :item
end
describe "#peek" do
it "should return the top element" do
@stack.peek.should == :item
end
end
end
16. Stack
#peek
should return the top element
shouldを消すとさらにドキュメントぽくなる
Stack
#peek
return the top element
Stack
#peek
先頭の要素を返す
17. サンプルを保留扱いにする
it "should be read all over"
it "should not be mixed with french fries" do
pending "cleaning out the fryer"
...
end
it "should be empty" do
pending ("bug report 18976") do
[].should be_empty
end
end
19. ヘルパーメソッド
describe Thing do
def create_thing(options) do
thing = Thing.new
thing.set_status(options[:status])
thing
end
it "should do something when ok" do
thing = create_thing(:status => "ok")
...
end
end
複数のサンプルグループで共有したい場合はmoduleでくくる
20. サンプルの共有
shared_examples_for "any pizza" do
it "tastes really good" do
@pizza.should taste_really_good
end
it "is available by the slice" do
@pizza.should be available_by_the_slice
end
end
21. describe "New York style thin crust pizza" do
before :each do
@pizza = Pizza.new(:region => "New York",
:style => "thin crust")
end
it behaves_like "any pizza"
it "has a really great sauce" do
@pizza.should have_a_really_great_sauce
end
end