I' on Micheal Hartl tutorial chapter 9 .And i'm trying to check for if the users name are displayed on the page . but i'm getting this error
Failure/Error: User.paginate.(page:1).each do |user|
ArgumentError:
wrong number of arguments (0 for 1)
i probably got to change this part User.paginate.(page: 1).each do |user|. but what do i have to do ?.
here the code inside the rspect directory.
describe "index" do
let(:user) { FactoryGirl.create(:user) }
before do
sign_in user
30.times { FactoryGirl.create(:user) }
visit users_path
end
it { should have_selector('title', text: 'All users') }
it { should have_selector('h1', text: 'All users') }
it "should list each user" do
User.paginate.(page: 1).each do |user|
page.should have_selector('li>a', text: user.name )
end
end
end
you have an extra period after paginate. It should be:
User.paginate(page: 1).each
Related
I'm trying to run a test on will_paginate. I know that it technically works, but I can't get the spec to work because of my inability to create multiple records. I'm using Capybara and Rspec on with Ruby on Rails.
Here is what I have in my feature spec.
RSpec.describe "Users Index", type: :feature do
describe "Pagination" do
let(:valid_user) { create(:user, name: "Mogli") }
let(:other_user) { create(:user, 50) }
it "successfully paginates" do
log_in_as_feature(valid_user)
visit users_path
puts URI.parse(current_url)
expect(page).to have_css('div.pagination')
expect(page).to have_link(href: user_path(valid_user), text: valid_user.name)
first_page_of_users = User.paginate(page: 1)
first_page_of_users.each do |user|
expect(page).to have_link(href: user_path(user), text: user.name)
unless user == valid_user
expect(page).to have_link(href: user_path(user), text: "delete")
end
end
end
end
end
My factory is simple:
FactoryGirl.define do
sequence(:name) { |n| "Person#{n}" }
factory :user do
name
email { "#{name}#example.com" }
password "foobar"
password_confirmation "foobar"
activated true
activated_at Time.zone.now
end
end
My fourth line is the culprit. It's not actually building the users. I tried to use a FactoryGirl.create(:user, 50), but that ends up breaking 27 tests across the board, and I have to reset the test database.
I don't know how else to create more than one dummy user at once, all the while keeping Mogli as first. Any insight is appreciated.
Edit: If I commented the have_css test, then my tests pass.
Here is the error of the div
1) Users Index Pagination successfully paginates
Failure/Error: expect(page).to have_css('div.pagination')
expected to find css "div.pagination" but there were no matches
# ./spec/features/users_index_spec.rb:13:inblock (3 levels) in '
Finished in 0.82368 seconds (files took 2.17 seconds to load)`
EDIT: adding my partial and index.html.erb view.
My view just renders #users partial
which is:
1 <li>
2 <%= gravatar_for user, size: 50 %>
3 <%= link_to user.name, user %>
4 <% if current_user.admin? && !current_user?(user) %>
5 | <%= link_to "delete", user, method: :delete,
6 data: { confirm: "You sure?" } %>
7 <% end %>
8
9 </li>
Your example has a few issues. As mentioned by others let is lazily evaluated, so to create objects that aren't directly referenced you would need to use let!. Additionally, FactoryGirl's create doesn't take a number of records to produce, you need create_list for that. Finally, Capybara's have_link takes the text of the link you're looking for as the first parameter so there's no need to pass a :text option
RSpec.describe "Users Index", type: :feature do
describe "Pagination" do
let!(:valid_user) { create(:user, name: "Mogli") }
let!(:other_users) { create_list(:user, 50) }
it "successfully paginates" do
log_in_as_feature(valid_user)
visit users_path
puts URI.parse(current_url)
expect(page).to have_css('div.pagination')
expect(page).to have_link(valid_user.name, href: user_path(valid_user))
User.paginate(page: 1).each do |user|
expect(page).to have_link(user.name, href: user_path(user))
expect(page).to have_link("delete", href: user_path(user)) unless user == valid_user
end
end
end
First of all, let is lazy-evaluated. That means it will not be evaluated until the moment you call it in your spec. You don't use other_user in your spec, so it is not evaluated.
Secondly, if you want to create a list of 50 users to set up your spec, use a before block:
before do
# using create_list
create_list(:user, 50)
# OR just
50.times { create(:user) }
end
it "successfully paginates" do
# ...
end
As mentioned already by Jan, let is lazily-evaluated. If you don't use other_user, it will never be created. There's an eager-counterpart, though.
let!(:other_user) { create(:user, 50) }
This one is always created.
I'm on the chapter 5 exercises for Michael Hartl's Rails Tutorial and am trying to wrap my head around how Rails/Rspec is testing a helper method full_title in app/helpers/application_helper.rb. All of my tests are in spec/requests/static_pages_spec.rb and within them, I'm calling full_title to cut down on code bloat.
So, in order to test the original full_title I create a test in spec/helpers/application_helpers_spec.rb and include it via spec/support/utilities.rb. The code is passing, but I want to understand the process (order to operations) to what is going on. Thank you.
Can I think of it like this?
Rspec begins to run static_pages_spec.rb (including utilities.rb)
Rspec sees the full_title method in static_pages_spec.rb
Rspec begins to run application_helper_spec.rb
Rspec sees describe "full_title" do in application_helper_spec.rb
Rspec looks up original full_title method and finishes test for application_helper_spec.rb
Rspec finishes tests in static_pages_spec.rb, iterating through above process whenfull_title` is called.
static_pages_spec.rb
require 'spec_helper'
describe "Static pages" do
subject { page }
shared_examples_for "all static pages" do
it { should have_selector('h1', text: heading) }
it { should have_selector('title', text: full_title(page_title)) }
end
describe "Home page" do
before { visit root_path }
let(:heading) { 'Sample App' }
let(:page_title) { '' }
it_should_behave_like "all static pages"
it { should_not have_selector 'title', text: '| Home' }
end
describe "Help page" do
before { visit help_path }
let(:heading) { 'Help' }
let(:page_title) { 'Help' }
it_should_behave_like "all static pages"
end
describe "About page" do
before { visit about_path }
let(:heading) { 'About' }
let(:page_title) { 'About Us' }
it_should_behave_like "all static pages"
end
describe "Contact page" do
before { visit contact_path }
let(:heading) { 'Contact' }
let(:page_title) { 'Contact' }
it_should_behave_like "all static pages"
end
it "should have the right links on the layout" do
visit root_path
click_link "About"
page.should have_selector 'title', text: full_title('About Us')
click_link "Help"
page.should have_selector 'title', text: full_title('Help')
click_link "Contact"
page.should have_selector 'title', text: full_title('Contact')
click_link "Home"
page.should have_selector 'title', text: full_title('')
click_link "Sign up now!"
page.should have_selector 'title', text: full_title('Sign up')
click_link "sample app"
page.should_not have_selector 'title', text: full_title('| Home')
end
end
application_helper_spec.rb
require 'spec_helper'
describe ApplicationHelper do
describe "full_title" do
it "should include the page title" do
full_title("foo").should =~ /foo/
end
it "should include the base title" do
full_title("foo").should =~ /^Ruby on Rails Tutorial Sample App/
end
it "should not include a bar for the home page" do
full_title("").should_not =~ /\|/
end
end
end
application_helper.rb
module ApplicationHelper
#Returns the full title on a per-page basis.
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
end
Think about it this way:
The 'full_title' called in static_pages_spec.rb (including utilities.rb) is running the 'full_title' method described in application_helper.rb.
The application_helper_spec.rb is validating the string/value (page_title) passed through full_title.
If I'm not mistaken, it does this each time the full_title method is called in your tests.
I am stuck and cannot figure out where I am going wrong. I have found similar topics but no solutions to these errors:
1) User pages delete links as an admin user admin visiting index page
Failure/Error: it { should have_link('delete', href: user_path(User.first)) }
expected link "delete" to return something
# ./spec/requests/user_pages_spec.rb:127:in `block (5 levels) in <top (required)>'
2) User pages delete links as an admin user admin visiting index page should be able to delete another user
Failure/Error: expect { click_link('delete') }.to change(User, :count).by(-1)
Capybara::ElementNotFound:
no link with title, id or text 'delete' found
# (eval):2:in `click_link'
# ./spec/requests/user_pages_spec.rb:129:in `block (6 levels) in <top (required)>'
# ./spec/requests/user_pages_spec.rb:129:in `block (5 levels) in <top (required)>'
Test Code
describe "delete links" do
it { should_not have_link('delete') }
describe "as an admin user" do
let(:admin) { FactoryGirl.create(:admin) }
before do
sign_in admin
visit users_path
end
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(admin)) }
end
end
Controller:
def
User.find(params[:id]).destroy
flash[:success] = "User destroyed."
redirect_to users_url
end
Index:
<% provide(:title, 'All users') %>
<h1>All users</h1>
<%= will_paginate %>
<ul class="users">
<%= render #users %>
</ul>
<%= will_paginate %>
Partial
<li>
<%= gravatar_for user, size: 52 %>
<%= link_to user.name, user %>
<% if current_user.admin? && !current_user?(user) %>
| <%= link_to "delete", user, method: :delete,
data: { confirm: "You sure?" } %>
<% end %>
</li>
Any guidance/help would be greatly appreciated....
Maybe I came one year later, but I just stated to learn Ruby on Rails from the same tutorial (Rails tutorial from Michael Hartl) and face on the same problem.
Here what I found and how can I did to fix (thanks to our mates commets, I went through the page source code and found the problem.):
From the tutorial, Listing 9.43 says:
require 'spec_helper'
describe "User pages" do
subject { page }
describe "index" do
let(:user) { FactoryGirl.create(:user) }
before do
sign_in user
visit users_path
end
it { should have_title('All users') }
it { should have_content('All users') }
describe "pagination" do
.
.
.
end
describe "delete links" do
it { should_not have_link('delete') }
describe "as an admin user" do
let(:admin) { FactoryGirl.create(:admin) }
before do
sign_in admin
visit users_path
end
it { should have_link('delete', href: user_path(User.first)) }
it "should be able to delete another user" do
expect do
click_link('delete', match: :first)
end.to change(User, :count).by(-1)
end
it { should_not have_link('delete', href: user_path(admin)) }
end
end end . . . end
Check how the section related to "delete links" is after the "pagination" section.
The pagination explanation starts in Listing 9.33, and has the next code:
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_title('All users') }
it { should have_content('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|
expect(page).to have_selector('li', text: user.name)
end
end
end end . . . end
I realize that the after(:all) { User.delete_all } statement erase ALL records from the test db after of that section (let me continue, because this is so obvious :P ). When the RSpec test continues to "delete links" there are any user created.
The next step is to create a new 'admin' user, and is the only one in the test DB at this point. As is expected the:
it { should have_link('delete', href: user_path(User.first)) }
will be fail because the current user can't delete itself, and it not has any 'delete' link. In the same way the next test will be fail too:
click_link('delete', match: :first)
The solution (Workaround or w/e :D ):
I believe there are some ways to resolve this, I think in two (no. 1 is the one that I used)
A. I add a non-admin user, before the admin user try to login, this let me have 2 users into the test DB, as the follow code:
describe "delete links" do
it { should_not have_link('delete') }
describe "as an admin user" do
let(:admin) { FactoryGirl.create(:admin) }
before do
FactoryGirl.create(:user, name: "test", email: "test#example.com")
sign_in admin
visit users_path
end
it { should have_link('delete', href: user_path(User.first)) }
it "should be able to delete another user" do
expect do
click_link('delete', match: :first)
end.to change(User, :count).by(-1)
end
it { should_not have_link('delete', href: user_path(admin)) }
end end
The statement FactoryGirl.create(:user, name: "test", email: "test#example.com") did the work.
B. Move all the "delete links" section before the "pagination" section, because I guess in that point, exists at least the first user created in the begin of the *user_pages_spec.rb*.
That's it, with this, I can get green the "delete links" new feature.
my two cents.
iVieL.
The first fixed test is right there ignore the rest. If your solution looks like the below you are golden. The second idea of moving the entire block above the pagination stuff also works.
describe 'delete links' do
it { should_not have_link('delete') }
describe 'as an admin user' do
let(:admin) { FactoryGirl.create(:admin) }
before do
FactoryGirl.create (:user)
sign_in admin
visit users_path
end
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(admin)) }
end
end
end
Im following [Michael Hartl's tutorial][1] and did the exercises in Chapter 7, and now have 4 errors that I cant figure out how to fix for the life of me. When I test the production app manually, the errors dont exist at all. So I don't know if there is something wrong with my text development or something, but Im at a total loss so I thought I'd post here to see if my total noobness is blinding me...thanks for your help!
Here's the 4 error messages I'm getting:
Failures:
1) signup with invalid information after submission
Failure/Error: it { should have_selector('title', text: "Sign up") }
expected css "title" with text "Sign up" to return something
# ./spec/requests/user_pages_spec.rb:38:in `block (4 levels) in <top (required)>'
2) signup with invalid information after submission
Failure/Error: it { should have_content('error') }
expected there to be content "error" in "after submission"
# ./spec/requests/user_pages_spec.rb:39:in `block (4 levels) in <top (required)>'
3) signup after saving the user
Failure/Error: it { should have_selector('title', text: user.name) }
NoMethodError:
undefined method `name' for nil:NilClass
# ./spec/requests/user_pages_spec.rb:60:in `block (3 levels) in <top (required)>'
4) signup after saving the user
Failure/Error: it { should have_selector('div.alert.alert-success', text: 'Welcome') }
expected css "div.alert.alert-success" with text "Welcome" to return something
# ./spec/requests/user_pages_spec.rb:61:in `block (3 levels) in <top (required)>'
Finished in 6.8 seconds
10 examples, 4 failures
Failed examples:
rspec ./spec/requests/user_pages_spec.rb:38 # signup with invalid information after submission
rspec ./spec/requests/user_pages_spec.rb:39 # signup with invalid information after submission
rspec ./spec/requests/user_pages_spec.rb:60 # signup after saving the user
rspec ./spec/requests/user_pages_spec.rb:61 # signup after saving the user
Here's the code on my user_pages_spec.rb:
require 'spec_helper'
require 'spec_helper'
describe "User pages" do
subject { page }
describe "signup page" do
before { visit signup_path }
it { should have_selector('h1', text: 'Sign up') }
it { should have_selector('title', text: full_title('Sign up')) }
end
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
before { visit user_path(user) }
it { should have_selector('h1', text: user.name) }
it { should have_selector('title', text: user.name) }
end
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_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
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
end
[1]: http://ruby.railstutorial.org/
Here's the template code for views/users/show.html.erb
<% provide(:title, #user.name) %>
<div class="row">
<aside class="span4">
<section>
<h1>
<%= gravatar_for #user %>
<%= #user.name %>
</h1>
</section>
</aside>
</div>
and then here's the users_controller.rb
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
end
def new
#user = User.new
end
def create
#user = User.new(params[:user])
if #user.save
sign_in #user
flash[:success] = "Welcome to the Sample App!"
redirect_to #user
else
render 'new'
end
end
end
Allright y'all,
I dont know if not answering my question was some sort of torturous initiation for noobs in this forum, but after almost 24 hours and a good night sleep, I solved the problem!
After a few G searches, I found that I could be stopping some of the variables from passing through by having "end" in the wrong places. It turns out there were 2 main areas where I was doing this. Once I found and fixed those, all the errors went away.
I will now pat myself on the back. I hope this helps any total noobs who run into this same problem in the future.
I'm new on Rails. I'm following the tutorial of Michael Hartl, Ruby on Rails Tutorial - Learn Web Development with Rails (http://ruby.railstutorial.org/). I've found an error when I try to pass some tests. My output when I execute bundle exec rspec is the next:
.........................................F.....
Failures:
1) Authentication signin with invalid information
Failure/Error: it { should have_selector('div.alert.alert-error', text: 'Invalid') }
expected css "div.alert.alert-error" with text "Invalid" to return something
# ./spec/requests/authentication_pages_spec.rb:23:in `block (4 levels) in <top (required)>'
Finished in 1.39 seconds
47 examples, 1 failure
Failed examples:
rspec ./spec/requests/authentication_pages_spec.rb:23 # Authentication signin with invalid information
The next files have been more highly changed and I think one of them may be causing the error:
authentication_pages_rspec.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') }
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
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
sessions_controler.rb
class SessionsController `>` ApplicationController
def new
end
def create
user = User.find_by_email(params[:session][:email])
if user && user.authenticate(params[:session][:password])
sign_in user
redirect_to user
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
end
def destroy
end
end
sessions_helper.rb
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
If you need any other file, please let me know and I'll post it. Thank you in advance.
I've browsed through your code, and apart from the semicolon in
self.current_user = user;
I haven't seen anything strange. You haven't posted the method you're testing,
if user && user.authenticate(params[:session][:password])
though, so the problem might be there.
General steps to find the issue:
restart spark if you're using it.
test the failing spec manually: does the functionality work?
if not, fix it
if the functionality should work, but the spec still fails, use launchy and save_and_open_page to peek at the page during your test.
Addition 1:
well, that part in your spec
before { visit signin_path }
describe "with invalid information" do
before { click_button "Sign in" }
leads you to the SessionController create action (because the link links to signin_path which is mapped there in config/routes.rb) and the main logic that could go wrong is user.authenticate(params[:session][:password]) - which is provided by has_secure_password.