RSpec は、同じ目的を達成するための有効な方法が常に多数あるため、最初は混乱する可能性があります。そのため、 「多くのチュートリアルと例」を参照する代わりに (またはその前に)、Rspec Bookを入手することをお勧めします。
この 1 つのテストに対して 8 つの異なる方法を簡単に思いつくことができ (以下のコード サンプルを参照)、それらの一部をさらに組み合わせて一致させることで、同じ問題を解決するためのさらに異なる方法を得ることができます。
これが、例とチュートリアルを実行する前に、RSpec の原則の基本的な理解を得ることが必要だと思う理由です。すべての例はわずかに異なって見えますが、基本を理解していれば、各例が何であるかを簡単に確認できます。します。
require "rspec"
class Song
def title=(title)
@title = title
end
def title
@title.capitalize
end
end
describe Song do
before { @song = Song.new }
describe "#title" do
it "should capitalize the first letter" do
@song.title = "lucky"
@song.title.should == "Lucky"
end
end
end
describe Song do
describe "#title" do
it "should capitalize the first letter" do
song = Song.new
song.title = "lucky"
song.title.should == "Lucky"
end
end
end
describe Song do
let(:song) { Song.new }
describe "#title" do
before { song.title = "lucky" }
it "should capitalize the first letter" do
song.title.should == "Lucky"
end
end
end
describe Song do
let(:song) { Song.new }
subject { song }
before { song.title = "lucky" }
its(:title) { should == "Lucky" }
end
describe Song do
let(:song) { Song.new }
describe "#title" do
before { song.title = "lucky" }
context "capitalization of the first letter" do
subject { song.title }
it { should == "Lucky" }
end
end
end
describe Song do
context "#title" do
before { subject.title = "lucky" }
its(:title) { should == "Lucky" }
end
end
RSpec::Matchers.define :be_capitalized do
match do |actual|
actual.capitalize == actual
end
end
describe Song do
context "#title" do
before { subject.title = "lucky" }
its(:title) { should be_capitalized }
end
end
describe Song do
let(:song) { Song.new }
context "#title" do
subject { song.title }
before { song.title = "lucky" }
it { should be_capitalized }
end
end