Eric Yang
Eric Yang

Reputation: 1889

Unexpected nil variable in 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

When I run it, though, rspec gives a NoMethodError because @affiliate is nil. What am I missing?

Upvotes: 2

Views: 208

Answers (1)

Mori
Mori

Reputation: 27799

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

Upvotes: 6

Related Questions