这里是新手...刚刚熟悉 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
我不知道如何满足“before do ....end” 我看到它被写在许多教程和 rspec 示例中,但我仍然不知道如何满足 ruby 代码以便它能够通过。谢谢
最佳答案
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
关于ruby - rspec newb 不明白如何满足 "before"方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14829630/