0

I've got some tests failing. The following are my tests:

require 'spec_helper'

describe "Authenticaton" do

  let(:base_title){"KS > Kids | "}

  subject { page }

  describe "for non-signed-in users" do

    let(:user) { FactoryGirl.create(:user) }
    let(:subject) { FactoryGirl.create(:subject) }

    context "in the Subjects controller" do

      describe "visiting the edit page" do
        before { visit edit_subject_path(subject) }
        it { should have_title("#{base_title}Home") }
      end 

      describe "visiting the subject index" do
        before { visit subjects_path }
        it { should have_title("#{base_title}Home") } 
      end 

    end 
  end 
end

And this is output

1) Authenticaton for non-signed-in users in the Subjects controller visiting the subject index

 Failure/Error: it { should have_title("#{base_title}#{I18n.t("messages.session.sign_in")}") }
 NoMethodError:
   undefined method `text' for nil:NilClass
 # ./spec/requests/extra_auth_pages_spec.rb:23:in `block (5 levels) in <top (required)>'

2) Authenticaton for non-signed-in users in the Subjects controller visiting the edit page

 Failure/Error: it { should have_title("#{base_title}Home") }
 NoMethodError:
   undefined method `text' for nil:NilClass
 # ./spec/requests/extra_auth_pages_spec.rb:18:in `block (5 levels) in <top (required)>'

I'm missing something here? Thanks for your help!

New Alexandria
  • 6,951
  • 4
  • 57
  • 77
B.I.
  • 706
  • 3
  • 9
  • 19

2 Answers2

2

It's hard to know without seeing the code under test, but there are a couple of potential causes:

1) Remember that let is lazy-evaluated: let does not create objects until the symbol is referenced in the spec. For this case, you may expect that the user instance is created before the visit occurs, but it won't exist because user is never evaluated. That will likely cause an authentication failure.

2) subject is an unfortunate choice of names, since RSpec uses subject to refer to the object instance being tested. This may or may not cause a problem.

zetetic
  • 47,184
  • 10
  • 111
  • 119
  • Thank you very much, my problem was name clash with subject in RSpec. I changed the variable name in let to something else, everything worked well. – B.I. Dec 13 '13 at 05:38
0

For the interest of future reference

I had the same problem, which was because some manually defined fixtures violated a uniqueness constraint in the model. I'm guessing this caused the fixtures to not be loaded correctly into the test database.

The test lines which failed were (I'm using RSpec):

it "should expect some value" do
    fixture_item = manual_fixture_table('fixture_item_label')
    expect (fixture_item).to be_valid    #This line was failing
    #[...]
end

The error I got was:

Failure/Error: expect(fixture_item).to be_valid
     
     NoMethodError:
       undefined method `text?' for nil:NilClass

By fixing this (in my case, by removing the uniqueness constraint which was unnecessary) this error was solved.

Lucas Franceschi
  • 398
  • 6
  • 12