gpt4 book ai didi

ruby-on-rails - Rspec:隐式定义的主题

转载 作者:行者123 更新时间:2023-12-02 00:20:59 27 4
gpt4 key购买 nike

Rspec implicitly defined subject documentation说:

While the examples below demonstrate how subject can be used as a user-facing concept, we recommend that you reserve it for support of custom matchers and/or extension libraries that hide its use from examples.

这是否意味着,我应该尽量不称呼“主题”。直接在我的规范?如果是,我应该使用什么作为主题对象?

最佳答案

比较这两个例子:

describe "User" do
subject { User.new(age: 42) }
specify { subject.age.should == 42 }
its(:age) { should == 42 }
end

describe "User" do
let(:user) { User.new(age: 42) }
specify { user.age.should == 42 }
end

更新

Rspec 中有一个很酷的特性——命名主题:

这是一个 example来自 David Chelimsky:

describe CheckingAccount, "with a non-zero starting balance" do
subject(:account) { CheckingAccount.new(Money.new(50, :USD)) }
it { should_not be_overdrawn }
it "has a balance equal to the starting balance" do
account.balance.should eq(Money.new(50, :USD))
end
end

当您使用 user 而不是 subject 时,它更具可读性(恕我直言)。但是 subject 让你可以使用很好的扩展 its(:age)

关于ruby-on-rails - Rspec:隐式定义的主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10988270/

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