RSpec中出现意外的nil变量

时间:2012-10-31 17:51:46

标签: ruby-on-rails rspec capybara

我有一个非常基本的RSpec示例无效。这是代码:

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是nil。我错过了什么?

1 个答案:

答案 0 :(得分:6)

before块中定义的实例变量在示例块(itspecify)中可见,但在describe块中不可见。 E.g:

specify 'URL should work' do
  visit @affiliate.aff_url
end