RSpec中意外的nil变量 [英] Unexpected nil variable in RSpec
本文介绍了RSpec中意外的nil变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个非常基本的RSpec示例,该示例不起作用。这是代码:
I have a very basic RSpec example that doesn't work. Here's the code:
require 'spec_helper'
describe "Referral-type functionality" do
describe "Affiliate system" do
before { @affiliate = Affiliate.create(account_id: 1) }
subject { @affiliate }
describe "URL should work" do
visit @affiliate.aff_url
end
end
end
但是,当我运行它时,rspec给出了NoMethodError,因为@affiliate为零。我缺少什么?
When I run it, though, rspec gives a NoMethodError because @affiliate is nil. What am I missing?
推荐答案
在之前
中定义的实例变量块在示例块中可见(它
,指定
),但在 describe <中不可见/ code>块。例如:
The instance variables defined in the before
block are visible in the example block (it
, specify
) but not in the describe
block. E.g.:
specify 'URL should work' do
visit @affiliate.aff_url
end
这篇关于RSpec中意外的nil变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文