Shared Example Groups
下記のようにshared_examples_forで定義したexample groupは、it_should_behave_likeメソッドを使うことで、複数の箇所で使い回せる。controllerのテストを書くときなどに使えそう(modelでは使う場面はないような気がする?)
shared_examples_for "All Employees" do it "should be payable" do @employee.should respond_to(:calculate_pay) end end shared_examples_for "All Managers" do it_should_behave_like "All Employees" it "should be bonusable" do @employee.should respond_to(:apply_bonus) end end describe Officer do before(:each) do @employee = Officer.new end it_should_behave_like "All Managers" it "should be optionable" do @employee.should respond_to(:grant_options) end end $ spec officer_spec.rb Officer - should be payable - should be bonusable - should be optionable