gpt4 book ai didi

ruby - rspec newb 不明白如何满足 "before"方法

转载 作者:数据小太阳 更新时间:2023-10-29 08:39:15 24 4
gpt4 key购买 nike

这里是新手...刚开始接触 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​​ 代码以便它通过。谢谢

最佳答案

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com