【发布时间】:2019-03-03 03:06:25
【问题描述】:
我的@attributes 变量有问题。我希望它可以访问以保持我的代码干燥,但目前,我必须重述变量并将其设置为“值”以使我的 rspec 测试正常工作。在不重复值的情况下,有什么更好的方法来做到这一点。
参考:Unexpected nil variable in RSpec
表明它在描述中不可访问,但需要另一种解决方案。 “指定”什么时候合适?我没用过。
describe "When one field is missing invalid " do
before(:each) do
@user = create(:user)
@attributes = {"has_car"=>"true", "has_truck"=>"true", "has_boat"=>"true", "color"=>"blue value", "size"=>"large value"}
end
values = {"has_car"=>"true", "has_truck"=>"true", "has_boat"=>"true", "color"=>"blue value", "size"=>"large value"}
values.keys.each do |f|
p = values.except(f)
it "returns invalid when #{f.to_s} is missing" do
cr = CarRegistration::Vehicle.new(@user, p)
cr.valid?
end
end
end
基于 cmets 的更新: 我还想在其他测试中使用值数组哈希。如果我按照说明将它放入循环中,我仍然需要在其他地方重复它。还有其他推荐吗?
更新:我尝试使用 let(),
describe "When one field is missing" do
let(:user) {Factorybot.create(:user)}
let(:attributes) = {{"has_car"=>"true", "has_truck"=>"true", "has_boat"=>"true", "color"=>"blue value", "size"=>"large value"}}
attributes do |f|
p = attributes.except(f)
it "returns invalid when #{f.to_s} is missing" do
cr = CarRegistration::Vehicle.new(user, p)
cr.valid?
end
end
end
但得到以下错误。
attributes 在示例组中不可用(例如,describe 或 context 块)。它只能从单个示例(例如it 块)或在示例范围内运行的构造(例如before、let 等)中获得。
【问题讨论】:
-
您可以在 it 块内部运行键的循环,这样您就不需要使用 'values' 变量复制 @attributes。
-
@BKSpureon 是否有关于在集团之外为其他人重用密钥的建议?
-
尝试使用 let 辅助方法以允许在块外重用:relishapp.com/rspec/rspec-core/docs/helper-methods/let-and-let
-
@BKSpureon,我试过 let 并得到上述错误。
-
请参阅下面我的回答,或者按照塞尔吉奥的回答。
标签: ruby-on-rails rspec-rails ruby-on-rails-5.2