I'm using ruby 1.8.7 on mac osx10.8. I have no idea what these errors mean. I am currently at the end of chapter 9 of the ruby on rails tutorial. Any help is greatly appreciated!
1) Authentication signin with invalid information
Failure/Error: before { click_button "Sign in" }
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/sessions_controller.rb:8:in `create'
# (eval):2:in `send'
# (eval):2:in `click_button'
# ./spec/requests/authentication_pages_spec.rb:21
2) Authentication signin with invalid information
Failure/Error: before { click_button "Sign in" }
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/sessions_controller.rb:8:in `create'
# (eval):2:in `send'
# (eval):2:in `click_button'
# ./spec/requests/authentication_pages_spec.rb:21
3) Authentication signin with invalid information after visiting another page
Failure/Error: before { click_button "Sign in" }
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/sessions_controller.rb:8:in `create'
# (eval):2:in `send'
# (eval):2:in `click_button'
# ./spec/requests/authentication_pages_spec.rb:21
4) Authentication signin with valid information
Failure/Error: before { sign_in user }
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/sessions_controller.rb:8:in `create'
# (eval):2:in `send'
# (eval):2:in `click_button'
# ./spec/support/utilities.rb:13:in `sign_in'
# ./spec/requests/authentication_pages_spec.rb:34
5) Authentication signin with valid information
Failure/Error: before { sign_in user }
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/sessions_controller.rb:8:in `create'
# (eval):2:in `send'
# (eval):2:in `click_button'
# ./spec/support/utilities.rb:13:in `sign_in'
# ./spec/requests/authentication_pages_spec.rb:34
6) Authentication signin with valid information
Failure/Error: before { sign_in user }
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/sessions_controller.rb:8:in `create'
# (eval):2:in `send'
# (eval):2:in `click_button'
# ./spec/support/utilities.rb:13:in `sign_in'
# ./spec/requests/authentication_pages_spec.rb:34
7) Authentication signin with valid information
Failure/Error: before { sign_in user }
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/sessions_controller.rb:8:in `create'
# (eval):2:in `send'
# (eval):2:in `click_button'
# ./spec/support/utilities.rb:13:in `sign_in'
# ./spec/requests/authentication_pages_spec.rb:34
8) Authentication signin with valid information
Failure/Error: before { sign_in user }
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/sessions_controller.rb:8:in `create'
# (eval):2:in `send'
# (eval):2:in `click_button'
# ./spec/support/utilities.rb:13:in `sign_in'
# ./spec/requests/authentication_pages_spec.rb:34
9) Authentication signin with valid information
Failure/Error: before { sign_in user }
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/sessions_controller.rb:8:in `create'
# (eval):2:in `send'
# (eval):2:in `click_button'
# ./spec/support/utilities.rb:13:in `sign_in'
# ./spec/requests/authentication_pages_spec.rb:34
10) Authentication signin with valid information followed by signout
Failure/Error: before { sign_in user }
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/sessions_controller.rb:8:in `create'
# (eval):2:in `send'
# (eval):2:in `click_button'
# ./spec/support/utilities.rb:13:in `sign_in'
# ./spec/requests/authentication_pages_spec.rb:34
11) Authentication authorization for non-signed-in users when attempting to visit a protected page after signing in should render the desired protected page
Failure/Error: click_button "Sign in"
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/sessions_controller.rb:8:in `create'
# (eval):2:in `send'
# (eval):2:in `click_button'
# ./spec/requests/authentication_pages_spec.rb:60
12) Authentication authorization for non-signed-in users when attempting to visit a protected page after signing in when signing in again should render the default (profile) page
Failure/Error: click_button "Sign in"
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/sessions_controller.rb:8:in `create'
# (eval):2:in `send'
# (eval):2:in `click_button'
# ./spec/requests/authentication_pages_spec.rb:60
13) Authentication authorization for non-signed-in users as wrong user visiting Users#edit page
Failure/Error: before { sign_in user }
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/sessions_controller.rb:8:in `create'
# (eval):2:in `send'
# (eval):2:in `click_button'
# ./spec/support/utilities.rb:13:in `sign_in'
# ./spec/requests/authentication_pages_spec.rb:106
14) Authentication authorization for non-signed-in users as wrong user submitting a PUT request to the Users#update action
Failure/Error: before { sign_in user }
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/sessions_controller.rb:8:in `create'
# (eval):2:in `send'
# (eval):2:in `click_button'
# ./spec/support/utilities.rb:13:in `sign_in'
# ./spec/requests/authentication_pages_spec.rb:106
15) User pages index
Failure/Error: sign_in user
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/sessions_controller.rb:8:in `create'
# (eval):2:in `send'
# (eval):2:in `click_button'
# ./spec/support/utilities.rb:13:in `sign_in'
# ./spec/requests/user_pages_spec.rb:12
16) User pages index
Failure/Error: sign_in user
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/sessions_controller.rb:8:in `create'
# (eval):2:in `send'
# (eval):2:in `click_button'
# ./spec/support/utilities.rb:13:in `sign_in'
# ./spec/requests/user_pages_spec.rb:12
17) User pages index pagination
Failure/Error: sign_in user
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/sessions_controller.rb:8:in `create'
# (eval):2:in `send'
# (eval):2:in `click_button'
# ./spec/support/utilities.rb:13:in `sign_in'
# ./spec/requests/user_pages_spec.rb:12
18) User pages index pagination should list each user
Failure/Error: sign_in user
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/sessions_controller.rb:8:in `create'
# (eval):2:in `send'
# (eval):2:in `click_button'
# ./spec/support/utilities.rb:13:in `sign_in'
# ./spec/requests/user_pages_spec.rb:12
19) User pages signup page with valid information edit page
Failure/Error: sign_in user
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/sessions_controller.rb:8:in `create'
# (eval):2:in `send'
# (eval):2:in `click_button'
# ./spec/support/utilities.rb:13:in `sign_in'
# ./spec/requests/user_pages_spec.rb:78
20) User pages signup page with valid information edit page
Failure/Error: sign_in user
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/sessions_controller.rb:8:in `create'
# (eval):2:in `send'
# (eval):2:in `click_button'
# ./spec/support/utilities.rb:13:in `sign_in'
# ./spec/requests/user_pages_spec.rb:78
21) User pages signup page with valid information edit page
Failure/Error: sign_in user
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/sessions_controller.rb:8:in `create'
# (eval):2:in `send'
# (eval):2:in `click_button'
# ./spec/support/utilities.rb:13:in `sign_in'
# ./spec/requests/user_pages_spec.rb:78
22) User pages signup page with valid information edit with invalid information
Failure/Error: sign_in user
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/sessions_controller.rb:8:in `create'
# (eval):2:in `send'
# (eval):2:in `click_button'
# ./spec/support/utilities.rb:13:in `sign_in'
# ./spec/requests/user_pages_spec.rb:78
23) User pages signup page with valid information edit with valid information
Failure/Error: sign_in user
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/sessions_controller.rb:8:in `create'
# (eval):2:in `send'
# (eval):2:in `click_button'
# ./spec/support/utilities.rb:13:in `sign_in'
# ./spec/requests/user_pages_spec.rb:78
24) User pages signup page with valid information edit with valid information
Failure/Error: sign_in user
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/sessions_controller.rb:8:in `create'
# (eval):2:in `send'
# (eval):2:in `click_button'
# ./spec/support/utilities.rb:13:in `sign_in'
# ./spec/requests/user_pages_spec.rb:78
25) User pages signup page with valid information edit with valid information
Failure/Error: sign_in user
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/sessions_controller.rb:8:in `create'
# (eval):2:in `send'
# (eval):2:in `click_button'
# ./spec/support/utilities.rb:13:in `sign_in'
# ./spec/requests/user_pages_spec.rb:78
26) User pages signup page with valid information edit with valid information
Failure/Error: sign_in user
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/sessions_controller.rb:8:in `create'
# (eval):2:in `send'
# (eval):2:in `click_button'
# ./spec/support/utilities.rb:13:in `sign_in'
# ./spec/requests/user_pages_spec.rb:78
27) User pages signup page with valid information edit with valid information
Failure/Error: sign_in user
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/sessions_controller.rb:8:in `create'
# (eval):2:in `send'
# (eval):2:in `click_button'
# ./spec/support/utilities.rb:13:in `sign_in'
# ./spec/requests/user_pages_spec.rb:78
my sessions controller:
class SessionsController < ApplicationController
def new
#title = "Sign in"
end
def create
user = User.find_by_email(params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
sign_in user
redirect_back_or user
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
end
def destroy
sign_out
redirect_to root_path
end
end
My User_pages_spec
require 'spec_helper'
describe "User pages" do
subject { page }
describe "index" do
let(:user) { FactoryGirl.create(:user) }
before(:each) do
sign_in user
visit users_path
end
it { should have_selector('title', :text => 'All users') }
it { should have_selector('h1', :text => 'All users') }
describe "pagination" do
before(:all) { 30.times { FactoryGirl.create(:user) } }
after(:all) { User.delete_all }
it { should have_selector('div.pagination') }
it "should list each user" do
User.paginate(:page => 1).each do |user|
page.should have_selector('li', :text => user.name)
end
end
end
end
describe "signup page" do
before { visit signup_path }
let(:submit) { "Create my account" }
it { should have_selector('h1', :text => 'Sign up') }
it { should have_selector('title', :text => full_title('Sign up')) }
describe "with invalid information" do
it "should not create a user" do
expect { click_button submit }.not_to change(User, :count)
end
describe "after submission" do
before { click_button submit }
it { should have_selector('title', :text => 'Sign up') }
it { should have_content('error') }
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
describe "after saving the user" do
before { click_button submit }
let(:user) { User.find_by_email('user#example.com') }
it { should have_selector('title', :text => user.name) }
it { should have_selector('div.alert.alert-success', :text => 'Welcome') }
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_selector('h1', :text => "Update your profile") }
it { should have_selector('title', :text => "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_selector('title', :text => new_name) }
it { should have_selector('div.alert.alert-success') }
it { should have_link('Sign out', :href => signout_path) }
specify { user.reload.name.should == new_name }
specify { user.reload.email.should == new_email }
end
end
end
end
end
My authentication_pages_spec:
require 'spec_helper'
describe "Authentication" do
subject { page }
describe "signin page" do
before { visit signin_path }
it { should have_selector('h1', :text => 'Sign in') }
it { should have_selector('h1','title', :text => 'Sign in') }
end
describe "signin" do
before { visit signin_path }
describe "with invalid information" do
before { click_button "Sign in" }
it { should have_selector('h1', 'title', :text => 'Sign in') }
it { should have_error_message }
describe "after visiting another page" do
before { click_link "Home" }
it { should_not have_error_message }
end
end
describe "with valid information" do
let(:user) { FactoryGirl.create(:user) }
before { sign_in user }
it { should have_selector('title', :text => user.name) }
it { should have_link('Profile', :href => user_path(user)) }
it { should have_link('Sign out', :href => signout_path) }
it { should have_link('Settings', :href => edit_user_path(user)) }
it { should have_link('Users', :href => users_path) }
it { should_not have_link('Sign in', :href => signin_path) }
describe "followed by signout" do
before { click_link "Sign out" }
it { should have_link('Sign in') }
end
end
end
describe "authorization" do
describe "for non-signed-in users" do
let(:user) { FactoryGirl.create(:user) }
describe "when attempting to visit a protected page" do
before do
visit edit_user_path(user)
fill_in "Email", :with => user.email
fill_in "Password", :with => user.password
click_button "Sign in"
end
describe "after signing in" do
it "should render the desired protected page" do
page.should have_selector('title', :text => 'Edit user')
end
describe "when signing in again" do
before do
click_link "Sign out"
click_link "Sign in"
fill_in "Email", :with => user.email
fill_in "Password", :with => user.password
click_button "Sign in"
end
it "should render the default (profile) page" do
page.should have_selector('title', :text => user.name)
end
end
end
end
describe "in the Users controller" do
describe "visiting the edit page" do
before { visit edit_user_path(user) }
it { should have_selector('title', :text => 'Sign in') }
it { should have_selector('div.alert.alert-notice') }
end
describe "submitting to the update action" do
before { put user_path(user) }
specify { response.should redirect_to(signin_path) }
end
describe "visiting the user index" do
before { visit users_path }
it { should have_selector('title', :text => 'Sign in') }
end
end
describe "as wrong user" do
let(:user) { FactoryGirl.create(:user) }
let(:wrong_user) { FactoryGirl.create(:user, :email => "wrong#example.com") }
before { sign_in user }
describe "visiting Users#edit page" do
before { visit edit_user_path(wrong_user) }
it { should_not have_selector('h1', 'title', :text => full_title('Edit user')) }
end
describe "submitting a PUT request to the Users#update action" do
before { put user_path(wrong_user) }
specify { response.should redirect_to(root_url) }
end
end
end
end
end
new,html.erb:
<% provide(:title, "Sign in") %>
<h1>Sign in</h1>
<div class="row">
<div class="span6 offset3">
<%= form_tag sessions_path do %>
<%= label_tag :email %>
<%= text_field_tag :email, params[:email] %>
<%= label_tag :password %>
<%= password_field_tag :password %>
<%= submit_tag "Sign in", :class => "btn btn-large btn-primary" %>
<% end %>
<p>New user? <%= link_to "Sign up now!", signup_path %></p>
</div>
</div>
The error is resulting from params[:session] being nil which in turn has resulted from your using form_tag instead of form_for in your view, presumably as a result of doing the first exercise at the end of Chapter 9. You can use form_tag, but you have to make the corresponding changes in SessionsController in keeping with RailsCast #270 (i.e. referencing the email address as params[:email] in your find_by_email call.).
Related
I am currently working on Chapter 8 of Michael Hartl's Ruby on Rails tutorial. I encountered a problem with my tests failing and tried to solve this myself, but I realize it's useless, so I have to ask here. Here is the error message I'm getting.
Failures:
1) Authentication signin with invalid information after visiting another page
Failure/Error: it { should_not have_selector('div.alert.alert-error') }
expected #has_selector?("div.alert.alert-error") to return false, got true
# ./spec/requests/authentication_pages_spec.rb:18:in `block (5 levels) in <top (required)>'
2) Authentication signin with valid information
Failure/Error: click_button "Sign in"
NoMethodError:
undefined method `sign_in' for #<SessionsController:0x007f8dbc9d5868>
# ./app/controllers/sessions_controller.rb:8:in `create'
# ./spec/requests/authentication_pages_spec.rb:27:in `block (4 levels) in <top (required)>'
3) Authentication signin with valid information
Failure/Error: click_button "Sign in"
NoMethodError:
undefined method `sign_in' for #<SessionsController:0x007f8dbfdf53a0>
# ./app/controllers/sessions_controller.rb:8:in `create'
# ./spec/requests/authentication_pages_spec.rb:27:in `block (4 levels) in <top (required)>'
4) Authentication signin with valid information
Failure/Error: click_button "Sign in"
NoMethodError:
undefined method `sign_in' for #<SessionsController:0x007f8dbfcf5ea0>
# ./app/controllers/sessions_controller.rb:8:in `create'
# ./spec/requests/authentication_pages_spec.rb:27:in `block (4 levels) in <top (required)>'
5) Authentication signin with valid information
Failure/Error: click_button "Sign in"
NoMethodError:
undefined method `sign_in' for #<SessionsController:0x007f8dbfc0ccc8>
# ./app/controllers/sessions_controller.rb:8:in `create'
# ./spec/requests/authentication_pages_spec.rb:27:in `block (4 levels) in <top (required)>'
6) User pages signup with valid information should create a user
Failure/Error: fill_in "Confirmation", with: "foobar"
Capybara::ElementNotFound:
Unable to find field "Confirmation"
# ./spec/requests/user_pages_spec.rb:24:in `block (4 levels) in <top (required)>'
Finished in 0.60967 seconds
41 examples, 6 failures
Failed examples:
rspec ./spec/requests/authentication_pages_spec.rb:18 # Authentication signin with invalid information after visiting another page
rspec ./spec/requests/authentication_pages_spec.rb:30 # Authentication signin with valid information
rspec ./spec/requests/authentication_pages_spec.rb:32 # Authentication signin with valid information
rspec ./spec/requests/authentication_pages_spec.rb:31 # Authentication signin with valid information
rspec ./spec/requests/authentication_pages_spec.rb:33 # Authentication signin with valid information
rspec ./spec/requests/user_pages_spec.rb:27 # User pages signup with valid information should create a user
Randomized with seed 17229
Here is my authentication pages spec file
require 'spec_helper'
describe "Authentication" do
subject { page }
describe "signin" do
before { visit signin_path }
describe "with invalid information" do
before { click_button "Sign in" }
it { should have_title('Sign in') }
it { should have_selector('div.alert.alert-error') }
describe "after visiting another page" do
before { click_link "Home" }
it { should_not have_selector('div.alert.alert-error') }
end
end
describe "with valid information" do
let(:user) { FactoryGirl.create(:user) }
before do
fill_in "Email", with: user.email.upcase
fill_in "Password", with: user.password
click_button "Sign in"
end
it { should have_title(user.name) }
it { should have_link('Profile', href: user_path(user)) }
it { should have_link('Sign out', href: signout_path) }
it { should_not have_link('Sign in', href: signin_path) }
end
end
end
Here is my sessions_controller.rb file
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
sign_in user
redirect_to user
else
# Create an error message and re-render the signin form
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
end
def destroy
end
end
The main failure I'm trying to solve, that should be green is 1. I passed the flash.now hash to indicate that I want the flash to disappear after a second request, but rspec says no.
I'm stumped as to what the problem is. Any help would be greatly appreciated
RSpec tells you that method sign_in does not exists. If you look at the chapter 8.2.2 you will find a helper with this method.
#app/helpers/sessions_helper.rb
module SessionsHelper
def sign_in(user)
remember_token = User.new_remember_token
cookies.permanent[:remember_token] = remember_token
user.update_attribute(:remember_token, User.digest(remember_token))
self.current_user = user
end
end
Can anyone who has done this exercises help me out?
It's my first time using this programming language and not much to this, but I need to finish well until chapter 10 that is a final evaluation.
1) User pages edit with valid information
Failure/Error: describe "page" do
NoMethodError:
undefined method `describe' for #<RSpec::Core::ExampleGroup::Nested_5::Nested_5::Nested_1:0xc283f8c>
# ./spec/requests/user_pages_spec.rb:87:in `block (3 levels) in <top (required)>'
2) User pages edit with valid information
Failure/Error: describe "page" do
NoMethodError:
undefined method `describe' for #<RSpec::Core::ExampleGroup::Nested_5::Nested_5::Nested_1:0xb67b1cc>
# ./spec/requests/user_pages_spec.rb:87:in `block (3 levels) in <top (required)>'
3) User pages edit with valid information
Failure/Error: describe "page" do
NoMethodError:
undefined method `describe' for #<RSpec::Core::ExampleGroup::Nested_5::Nested_5::Nested_1:0xc224244>
# ./spec/requests/user_pages_spec.rb:87:in `block (3 levels) in <top (required)>'
4) User pages edit with valid information
Failure/Error: describe "page" do
NoMethodError:
undefined method `describe' for #<RSpec::Core::ExampleGroup::Nested_5::Nested_5::Nested_1:0xc562690>
# ./spec/requests/user_pages_spec.rb:87:in `block (3 levels) in <top (required)>'
5) User pages edit with valid information
Failure/Error: describe "page" do
NoMethodError:
undefined method `describe' for #<RSpec::Core::ExampleGroup::Nested_5::Nested_5::Nested_1:0xc0559a4>
# ./spec/requests/user_pages_spec.rb:87:in `block (3 levels) in <top (required)>'
6) User pages index
Failure/Error: visit users_path
ActionView::Template::Error:
undefined method `each' for nil:NilClass
# ./app/views/users/index.html.erb:5:in `_app_views_users_index_html_erb__615083638_97671840'
# ./spec/requests/user_pages_spec.rb:12:in `block (3 levels) in <top (required)>'
7) User pages index
Failure/Error: visit users_path
ActionView::Template::Error:
undefined method `each' for nil:NilClass
# ./app/views/users/index.html.erb:5:in `_app_views_users_index_html_erb__615083638_97671840'
# ./spec/requests/user_pages_spec.rb:12:in `block (3 levels) in <top (required)>'
8) User pages index should list each user
Failure/Error: visit users_path
ActionView::Template::Error:
undefined method `each' for nil:NilClass
# ./app/views/users/index.html.erb:5:in `_app_views_users_index_html_erb__615083638_97671840'
# ./spec/requests/user_pages_spec.rb:12:in `block (3 levels) in <top (required)>'
9) Authentication authorization after signing in should render the desired protected page
Failure/Error: expect(page).to have_title('Edit user')
expected #has_title?("Edit user") to return true, got false
# ./spec/requests/authentication_pages_spec.rb:84:in `block (4 levels) in <top (required)>'
Finished in 2.25 seconds
72 examples, 9 failures
Failed examples:
rspec ./spec/requests/user_pages_spec.rb:114 # User pages edit with valid information
rspec ./spec/requests/user_pages_spec.rb:115 # User pages edit with valid information
rspec ./spec/requests/user_pages_spec.rb:112 # User pages edit with valid information
rspec ./spec/requests/user_pages_spec.rb:113 # User pages edit with valid information
rspec ./spec/requests/user_pages_spec.rb:111 # User pages edit with valid information
rspec ./spec/requests/user_pages_spec.rb:15 # User pages index
rspec ./spec/requests/user_pages_spec.rb:16 # User pages index
rspec ./spec/requests/user_pages_spec.rb:18 # User pages index should list each user
rspec ./spec/requests/authentication_pages_spec.rb:83 # Authentication authorization after signing in should render the desired protected page
Edit
Here is my code:
require 'spec_helper'
describe "User pages" do
subject { page }
describe "index" do
before do
sign_in FactoryGirl.create(:user)
FactoryGirl.create(:user, name: "Bob", email: "bob#example.com")
FactoryGirl.create(:user, name: "Ben", email: "ben#example.com")
visit users_path
end
it { should have_title('All users') }
it { should have_content('All users') }
it "should list each user" do
User.all.each do |user|
expect(page).to have_selector('li', text: user.name)
end
end
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 page" do
before { visit signup_path }
it { should have_content('Sign up') }
it { should have_title(full_title('Sign up')) }
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
describe "after submission" do
before { click_button submit }
it { should have_title('Sign up') }
it { should have_content('error') }
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
describe "after saving the user" do
before { click_button submit }
let(:user) { User.find_by(email: 'user#example.com') }
it { should have_link('Sign out') }
it { should have_title(user.name) }
it { should have_selector('div.alert.alert-success', text: 'Welcome') }
end
end
end
describe "edit" do
let(:user) { FactoryGirl.create(:user) }
before do
sign_in user
visit edit_user_path(user)
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
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
If your code is formatted like this:
describe "edit" do
let(:user) { FactoryGirl.create(:user) }
before do
sign_in user
visit edit_user_path(user)
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
I can see that it is probably broken without reading any of your code.
Are all your code blocks properly closed? Does not look so!
In addition to the unclosed blocks that phoet pointed out, which are causing your "undefined method describe" errors, you're getting errors in /app/views/users/index.html.erb line 5. The error is undefined method `each' for nil:NilClass which means there's a variable on that line that you're doing variable.each with, but you're either not setting that variable or when you're setting it, it's getting set to nil.
In general, the way for you to successfully get through this is to read the error messages and try to fix what they're complaining about. If you search for each error message individually, you'll find stack overflow questions about the specific issue that will help you.
I am following Hartl's Rails tutorial but I used devise/cancan/rolify so some things are not the same.
On my users_pages_spec.rb test, I keep getting this error in spite of all I tried:
1) User pages index delete links as an admin user
Failure/Error: sign_in #superadmin
NoMethodError:
undefined method `email' for nil:NilClass
# ./spec/support/utilities.rb:14:in `sign_in'
# ./spec/requests/user_pages_spec.rb:91:in `block (5 levels) in <top (required)>'
2) User pages index delete links as an admin user
Failure/Error: sign_in #superadmin
NoMethodError:
undefined method `email' for nil:NilClass
# ./spec/support/utilities.rb:14:in `sign_in'
# ./spec/requests/user_pages_spec.rb:91:in `block (5 levels) in <top (required)>'
3) User pages index delete links as an admin user should be able to delete another user
Failure/Error: sign_in #superadmin
NoMethodError:
undefined method `email' for nil:NilClass
# ./spec/support/utilities.rb:14:in `sign_in'
# ./spec/requests/user_pages_spec.rb:91:in `block (5 levels) in <top (required)>'
4) User pages index delete links as an admin user
Failure/Error: sign_in #superadmin
NoMethodError:
undefined method `email' for nil:NilClass
# ./spec/support/utilities.rb:14:in `sign_in'
# ./spec/requests/user_pages_spec.rb:91:in `block (5 levels) in <top (required)>'
I don't understand why, does nil::nilClass mean factory girl actually does not manage to create this superadmin user?
Here is the sample of my user_pages_spec.rb file:
require 'spec_helper'
require 'cancan/matchers'
# Test if targeted words appears in displayed content
describe "User pages" do
subject { page }
let(:user) { FactoryGirl.create(:user) }
let(:wrong_user) { FactoryGirl.create(:user, email: "wrong#example.com") }
let(:non_admin) { FactoryGirl.create(:user) }
let(:submit) { "Sign in" }
describe "index" do
describe "delete links" do
it { should_not have_link('delete') }
describe "as an admin user" do
let(:superadmin) { FactoryGirl.create(:superadmin) }
before do
sign_in #superadmin
visit users_path
end
it { should have_title('List of users') }
it { should have_link('delete', href: user_path(User.first)) }
it "should be able to delete another user" do
expect { click_link('delete') }.to change(User, :count).by(-1)
end
it { should_not have_link('delete', href: user_path(superadmin)) }
end
end
end
end
my spec/utilities.rb defining sign_in method
include ApplicationHelper
def sign_in(user)
visit new_user_session_path
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Login"
#populate cookie when not using capybara
cookies[:authentication_token] = user.authentication_token
end
and my spec/factories/user.rb
FactoryGirl.define do
factory :user do
sequence(:name) { |n| "Person #{n}" }
sequence(:email) { |n| "person_#{n}#example.com"}
password "beta"
password_confirmation "beta"
# required if the Devise Confirmable module is used
confirmed_at Time.now
confirmation_token nil
factory :superadmin do
after(:create) {|user| user.add_role(:superadmin)}
end
end
end
I do have in cancan/rolify a superadmin role defined.
If anybody has a clue, I'd take it! i can update the question with additional file needed if not already here.
Your let call is setting superadmin, but you're referencing #superadmin, which is going to be nil, since it hasn't been set. BTW, this is a good reason to generally avoid using instance variables in your tests, as they silently default to nil when referenced if uninitialized.
I've been having trouble trying to get my test to pass. I can't find what the issue is any help would be greatly appreciated!
Here is the failure message.
1) Authentication signin with valid information
Failure/Error: click_button "Sign in"
NoMethodError:
undefined method `remember_token' for #<Class:0x007fe30d0a93b0>
# ./app/helpers/sessions_helper.rb:4:in `sign_in'
# ./app/controllers/sessions_controller.rb:9:in `create'
# (eval):2:in `click_button'
# ./spec/requests/authentication_pages_spec.rb:34:in `block (4 levels) in <top (required)>'
2) Authentication signin with valid information
Failure/Error: click_button "Sign in"
NoMethodError:
undefined method `remember_token' for #<Class:0x007fe30d0a93b0>
# ./app/helpers/sessions_helper.rb:4:in `sign_in'
# ./app/controllers/sessions_controller.rb:9:in `create'
# (eval):2:in `click_button'
# ./spec/requests/authentication_pages_spec.rb:34:in `block (4 levels) in <top (required)>'
3) Authentication signin with valid information
Failure/Error: click_button "Sign in"
NoMethodError:
undefined method `remember_token' for #<Class:0x007fe30d0a93b0>
# ./app/helpers/sessions_helper.rb:4:in `sign_in'
# ./app/controllers/sessions_controller.rb:9:in `create'
# (eval):2:in `click_button'
# ./spec/requests/authentication_pages_spec.rb:34:in `block (4 levels) in <top (required)>'
4) Authentication signin with valid information
Failure/Error: click_button "Sign in"
NoMethodError:
undefined method `remember_token' for #<Class:0x007fe30d0a93b0>
# ./app/helpers/sessions_helper.rb:4:in `sign_in'
# ./app/controllers/sessions_controller.rb:9:in `create'
# (eval):2:in `click_button'
# ./spec/requests/authentication_pages_spec.rb:34:in `block (4 levels) in <top (required)>'
Heres the authentication pages spec rb:
require 'spec_helper'
describe "Authentication" do
subject { page }
describe "signin page" do
before { visit signin_path }
it { should have_selector('h1', text: 'Sign in') }
it { should have_selector('title', text: 'Sign in') }
end
describe "signin" do
before { visit signin_path }
describe "with invalid information" do
before { click_button "Sign in" }
it { should have_selector('title', text: 'Sign in') }
it { should have_selector('div.alert.alert-error', text: 'Invalid') }
end
describe "after visiting another page" do
before { click_link "Home" }
it { should_not have_selector('div.alert.alert-error') }
end
describe "with valid information" do
let(:user) { FactoryGirl.create(:user) }
before do
fill_in "Email", with: user.email.upcase
fill_in "Password", with: user.password
click_button "Sign in"
end
it { should have_selector('title', text: user.name) }
it { should have_link('Profile', href: user_path(user)) }
it { should have_link('Sign out', href: signout_path) }
it { should_not have_link('Sign in', href: signin_path) }
end
end
end
Here is the session helper
module SessionsHelper
def sign_in(user)
cookies.permanent[:remember_token] = user.remember_token
self.current_user = user
end
def signed_in?
!current_user.nil?
end
def current_user=(user)
#current_user = user
end
def current_user
#current_user ||= User.find_by_remember_token(cookies[:remember_token])
end
end
Here is the user rb file
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
before_save { |user| user.email = email.downcase }
before_save :create_remember_token
validates :name, presence: true, length: {maximum: 50}
VALID_EMAIL_REGEX = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, length: {minimum: 6}
validates :password_confirmation, presence:true
private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
Here is the sessions controller
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by_email(params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
sign_in User
redirect_to user
else
flash.now[:error] = 'Invalid email/password combination' # Not quite right!
render 'new'
end
end
def destroy
end
end
These are the files I think the issues are with let me know if you need to see any other files. Any thoughts would be greatly appreciated! thanks!
[Updated per comment thread from original answer].
The error message shows a Class as missing the method, which means that a Class rather than an instance of User was passed as the argument to sign_in. Checking the calling code in sessions_controller.rb, you can see that User was passed instead of user.
In general, I found that tutorial to be "spot on". If you follow the text carefully, you won't go wrong.
I'm working on RoR tutorial, Chapter 8. I am trying to complete section 8.2.4 - Changing the layout links and ran $ bundle exec rspec spec/ and I got the following error:
Failures:
1) Authentication signin with valid information
Failure/Error: click_button "Sign in"
NoMethodError:
undefined method `sign_in' for #<SessionsController:0x007fef66b85fd0>
# ./app/controllers/sessions_controller.rb:9:in `create'
# (eval):2:in `click_button'
# ./spec/requests/authentication_pages_spec.rb:35:in `block (4 levels) in <top (required)>'
2) Authentication signin with valid information
Failure/Error: click_button "Sign in"
NoMethodError:
undefined method `sign_in' for #<SessionsController:0x007fef66b33eb0>
# ./app/controllers/sessions_controller.rb:9:in `create'
# (eval):2:in `click_button'
# ./spec/requests/authentication_pages_spec.rb:35:in `block (4 levels) in <top (required)>'
3) Authentication signin with valid information
Failure/Error: click_button "Sign in"
NoMethodError:
undefined method `sign_in' for #<SessionsController:0x007fef6698bba8>
# ./app/controllers/sessions_controller.rb:9:in `create'
# (eval):2:in `click_button'
# ./spec/requests/authentication_pages_spec.rb:35:in `block (4 levels) in <top (required)>'
4) Authentication signin with valid information
Failure/Error: click_button "Sign in"
NoMethodError:
undefined method `sign_in' for #<SessionsController:0x007fef68286130>
# ./app/controllers/sessions_controller.rb:9:in `create'
# (eval):2:in `click_button'
# ./spec/requests/authentication_pages_spec.rb:35:in `block (4 levels) in <top (required)>'
Finished in 1.34 seconds
44 examples, 4 failures
Failed examples:
rspec ./spec/requests/authentication_pages_spec.rb:39 # Authentication signin with valid information
rspec ./spec/requests/authentication_pages_spec.rb:40 # Authentication signin with valid information
rspec ./spec/requests/authentication_pages_spec.rb:38 # Authentication signin with valid information
rspec ./spec/requests/authentication_pages_spec.rb:41 # Authentication signin with valid information
Based on the output, I checked spec/requests/authentication_pages_spec.rb, but I can't seem to find the error. This is what I have:
require 'spec_helper'
describe "Authentication" do
subject { page }
describe "signin page" do
before { visit signin_path }
it { should have_selector('h1', text: 'Sign in') }
it { should have_selector('title', text: 'Sign in') }
end
describe "signin" do
before { visit signin_path }
describe "with invalid information" do
before { click_button "Sign in" }
it { should have_selector('title', text: 'Sign in') }
it { should have_selector('div.alert.alert-error', text: 'Invalid') }
describe "after visiting antoher page" do
before { click_link "Home" }
it { should_not have_selector('div.alert.alert-error') }
end
end
describe "with valid information" do
let(:user) { FactoryGirl.create(:user) }
before do
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
end
it { should have_selector('title', text: user.name) }
it { should have_link('Profile', href: user_path(user)) }
it { should have_link('Sign out', href: signout_path) }
it { should_not have_link('Sign in', href: signin_path) }
end
end
end
I can't seem to find the error. Any suggestions on where I should look based on the error output? Thank you.
Check your config/routes.rb file to make sure that:
match '/signin', to: 'sessions#new'
matches what your session controllers is seeing. The create method for your "sessions_controller.rb" should have:
def create
user = User.find_by_email(params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
sign_in user
redirect_back_or user
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
end
Just my guess. I'm currently around that area too in the tutorial. Best of luck and I hope that helps!
A bit late probably but anyways...
Check if you got "include SessionsHelper" in app/controllers/application_controller.rb
helpers are included into views by default but you have to explicitly include them in controllers