0

I'm stuck at Chapter 9 in the Rails tutorial - more specifically at the end of section 9.1. My problem is similar to the one in this thread but the solution there didn't work for me.

Here is my user_pages_spec.rb:

require 'spec_helper'
describe "User pages" do
subject { page }

describe "signup page" do
before { visit signup_path }

it { should have_content('Sign up') }
it { should have_title(full_title('Sign up')) }
end

describe "profile page" do
let (:user) {FactoryGirl.create(:user)}
before { visit user_path(user) }

it { should have_content(user.name) }
it { should have_title(user.name) }
end

describe "signup" do

before { visit signup_path }

let(:submit) { "Create my account" }

describe "with invalid information" do
  it "should not create a user" do
    expect { click_button submit }.not_to change(User, :count)
  end
end

describe "with valid information" do
  before do
    fill_in "Name",         with: "Example User"
    fill_in "Email",        with: "user@example.com"
    fill_in "Password",     with: "foobar"
    fill_in "Confirmation", with: "foobar"
  end

  it "should create a user" do
    expect { click_button submit }.to change(User, :count).by(1)
  end
end
end

describe "edit" do
let(:user) { FactoryGirl.create(:user)}
before do
  sign_in user
  visit edit_user_path(user)
end

describe "page" do
  it { should have_content("Update your profile")}
  it { should have_title("Edit user")}
  it { should have_link('change', href:'http://gravatar.com/emails')}
end

describe "with invalid information" do
  before { click_button "Save changes"}

  it { should have_content('error') }
end

describe "with valid information" do
  let(:new_name) { "New Name"}
  let(:new_email) {new@example.com}
  before do
    fill_in "Name", with: new_name
    fill_in "Email", with: new_email
    fill_in "Password", with: user.password
    fill_in "Confirm Password", with: user.password
    click_button "Save changes"
  end

  it {should have_title(new_name)}
  it {should have_selector('div.alert.alert-success')}
  it {should have_link('Sign out', href: signout_path)}
  specify {expect(user.reload.name).to eq new_name}
  specify {expect(user.reload.email).to eq new_email}
end
end
end

Here is the error message:

bundle exec rspec spec/
.............................................FFFFFFFFF

Failures:

 1) User pages edit page 
 Failure/Error: sign_in user
 NoMethodError:
   undefined method `sign_in' for #<RSpec::Core::ExampleGroup::Nested_4::Nested_4::Nested_1:0x007faa37859d80>
 # ./spec/requests/user_pages_spec.rb:49:in `block (3 levels) in <top (required)>'

And here is my spec/support/utilities.rb:

def full_title(page_title)
base_title = "Ruby on Rails Tutorial Sample App"
if page_title.empty?
base_title
else
"#{base_title} | #{page_title}"
end
end

def sign_in (user, options={})
if options[:no_capybara]
# Sign in when not using Capybara
remember_token = user.new_remember_token
cookies[:remember_token]
user.update_attribute(:remember_token, User.digest(remember_token))
else
visit signin_path
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
end
end

Any suggestions?

Community
  • 1
  • 1
Nick
  • 2,924
  • 4
  • 36
  • 43
  • i suspect you did not require or include `spec/support/utilities.rb` specifically at any point in your spec-helper? – phoet May 21 '14 at 00:44
  • Actually, I did. There is a line in the spec-helper: `Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}` – Nick May 21 '14 at 00:54
  • then this should just work. i always use modules to manage such things: https://github.com/phoet/on_ruby/blob/master/spec/spec_helper.rb#L13-L18 – phoet May 21 '14 at 01:02
  • Thanks for the suggestion - I decided to faithfully follow the tutorial as it is my first in rails so thought it will minimise situations like this one. I have really no clue why the `sign_in` method is not recognised. – Nick May 21 '14 at 01:08

1 Answers1

0

I did run bundle install while I was trying to install rcov (Rubymine was complaining about its lack). The installation failed as rcov is not available for my version of rails. That's fine.

The really bizarre thing is that afterwards I re-ran the tests and everything worked. There was no trace of the error message. I am a rails noob but this is a bit rich - a failed bundle install shall not change anything. There was no reason for the error message I observed and now it disappeared without any reason.

==EDIT: I realised the problem is that rails does not seem to empty the cache between tests (which is, in my opinion, a scary bug). By default it fails to re-read the files and thus may ignore changes that have occurred. I put more details here: Rails tutorial: undefined method

Community
  • 1
  • 1
Nick
  • 2,924
  • 4
  • 36
  • 43