Newb在这里......刚刚与rspec站在一起,并对以下内容提出疑问:
describe Song do
before do
@song = Song.new
end
describe 'title' do
it 'should capitalize the first letter' do
@song.title = "lucky"
@song.title.should == "Lucky"
end
end
我不确定如何满足“以前做......结束” 我看到它写在许多教程和rspec示例中,但我仍然失去了如何满足ruby代码,以便它将通过。感谢
答案 0 :(得分:4)
RSpec起初可能会令人困惑,因为总有很多有效的方法可以实现相同的目标。这就是为什么我建议您通过“许多教程和示例”而不是(或之前)获得The Rspec Book。
我可以通过八种不同的方式轻松实现这一单一测试(参见下面的代码示例),您可以将它们的部分混合搭配得更多,从而产生更多不同的方法来解决同样的问题。
这就是为什么我认为在你能够完成示例和教程之前必须对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