I am using Ruby 1.9.2 and Rails 3.0.3
I am consistently getting failing test errors within this tutorial such as:
1) UsersController GET 'show' should show the user's micropost
Failure/Error: get :show, :id => #user
undefined method `model_name' for NilClass:Class
# ./app/views/users/show.html.erb:10:in `_app_views_users_show_html_erb__2659465448390875355_2173812560_4463455946834160058'
# ./spec/controllers/users_controller_spec.rb:39:in `block (3 levels) in <top (required)>'
show.html.erb:10 is:
<% unless #user.microposts.empty? %>
<table class="microposts" summary="User microposts">
<%= render #microposts %> #line 10
</table>
<%= will_pagination #microposts %>
<% end %>
users_controller_spec.rb:39 is:
describe "GET 'show'" do
before(:each) do
#user = Factory(:user)
end
.
.
.
it "should show the user's micropost" do
mp1 = Factory(:micropost, :user => #user, :content => "Foo bar")
mp2 = Factory(:micropost, :user => #user, :content => "Baz quux")
get :show, :id => #user
response.should have_selector("span.content", :content => mp1.cntent)
response.should have_selector("span.content", :content => mp2.content)
end
and my app/controllers/users_controller.rb where is defines micropost is:
def show
#user = User.find(params[:id])
#microposts = #user.microposts.paginate(:page => params[:page])
#title = #user.name
end
any help would be greatly appreciated. I have a total of 14 errors. My other errors are about
Failure/Error: get :edit, :id => #user
#controller is nil: make sure you set it in your test's setup method.
and
Failure/Error: test_sign_in(wrong_user)
undefined method `sign_in' for nil:NilClass
I am also using Factory as a test sign in. Im not sure if this could be a problem. I mention this because some of the errors are:
9) authentication of edit/update pages GET 'index' for signed-in users should have the right title
Failure/Error: #user = test_sign_in(Factory(:user))
Validation failed: Email has already been taken
# ./spec/controllers/users_controller_spec.rb:222:in `block (4 levels) in <top (required)>'
If you need to see anymore code please let me know.
If someone could direct me to a place to learn more about debugging, that would be a big help as well.
For error 1 try checking your filenames for spelling and case.
For error 9 try double checking which do..end you paste your code into.
Working code for the tutorial can be found here https://github.com/railstutorial
Related
I am trying to write a controller spec to test that the right partial is rendering after a post request.
Here is the controller method being posted to:
def lookup
#guest = Guest.where("mobile_number = ?", params[:lookup_mobile_phone_number]).first_or_initialize do |g|
g.mobile_number = params[:lookup_mobile_phone_number]
end
if #guest.new_record?
#visit = Visit.new(hotel_id: params[:hotel_id])
render partial: "guests/form"
else
#visit = Visit.new(guest_id: #guest.id, hotel_id: params[:hotel_id])
render partial: "visits/form"
end
end
Here is the spec/controllers/guests_controller_spec.rb I wrote that is failing:
RSpec.describe GuestsController, :type => :controller do
describe "#lookup" do
render_views
let!(:returning_guest) { create(:test_guest) }
context "when guest is already registered with hotel" do
it "renders visits/form" do
post :lookup, :guest => { :lookup_mobile_phone_number => "5553331212"}
expect(response).to render_template(:partial => 'visits/form')
end
end
end
end
Here is the factory I'm using for :test_guest
FactoryGirl.define do
factory :test_guest, :class => 'Guest' do
name 'Jack Guest'
mobile_number '5553331212'
end
end
This is the response I am getting when the test fails
1) GuestsController#lookup when guest is already registered with hotel renders visits/form
Failure/Error: expect(response).to render_template(:partial => 'visits/form')
expecting partial <visits/form> but action rendered <["shared/_hotel_agent_name", "_hotel_agent_name", "guests/_form", "_form"]>.
Expected {"shared/_hotel_agent_name"=>1, "_hotel_agent_name"=>1, "guests/_form"=>1, "_form"=>1} to include "visits/form".
# ./spec/controllers/guests_controller_spec.rb:16:in `block (4 levels) in <top (required)>'
I've been hacking away at this a for a few days now, trying different approaches found on here with no luck. Any help would be much appreciated :)
You send
post :lookup, :guest => { :lookup_mobile_phone_number => "5553331212"}
but in controller, you use
params[:lookup_mobile_phone_number]
not
params[:guest][:lookup_mobile_phone_number]
So to fix it, according to your controller, do
post :lookup, :lookup_mobile_phone_number => "5553331212"
I'm trying to test an existing rails project with rspec. And I want to test a controller but getting an error which I can't solve :S
Here is the my spec code ;
require 'spec_helper'
describe BriefNotesController do
before(:all) do
#customer=Factory(:customer)
#project=Factory(:project_started, :owner => #customer)
end
context 'get :new' do
it 'should redirect to login page for not signed in users' do
get :new, :project_id => #project.id
response.should redirect_to("/kullanici-girisi")
end
it 'should be success and render new brief note page for project owner' do
sign_in #customer
get :new, :project_id => #project.id
response.should be_success
end
end
end
Here is the my controller code ;
class BriefNotesController < ApplicationController
before_filter :authenticate_user!
before_filter :find_project
def new
#brief_note = #project.brief_notes.new
end
def create
#brief_note = #project.brief_notes.build(params[:brief_note])
if #brief_note.save
redirect_to brief_project_path(#project)
else
render :action => :new
end
end
private
def find_project
#project = current_user.projects.find_by_cached_slug([params[:project_id]])
end
end
I think current_user.projects.find_by_cached_slug method don't work. So this is the error;
Failures:
1) BriefNotesController get :new should be success and render new brief note page for project owner
Failure/Error: get :new, :project_id => #project.id
NoMethodError:
undefined method `brief_notes' for nil:NilClass
# ./app/controllers/brief_notes_controller.rb:6:in `new'
# ./spec/controllers/brief_notes_controller_spec.rb:19:in `block (3 levels) in <top (required)>'
I can't say for certain without more information about your models, but a likely culprit is that you're passing #project.id as the request parameter, but you're doing the lookup by cached_slug. Try #project.to_param instead.
The error is coming from your find_project filter: find_by_cached_slug is returning a nil, which is assigned to #project, and that triggers the undefined method error when brief_notes is called on it (in the new action).
From your spec description I assume that it shouldn't even be executing the new code, and instead redirecting in authenticate_user!? I don't use devise myself (this is a devise method, right?) so I'm not sure of the specifics of that method, but I think that's where your problem is coming from.
I don't think the problem is your FactoryGirl syntax, which is deprecated but should still work.
I'm on section 10.3.1 "User Index" of Ruby on Rails 3 Tutorial. I have one test that refuses to pass and I have researched the error extensively and can't seem to get it to pass.
Here is the error:
1) UsersController GET 'index' for signed-in users should have an element for each user
Failure/Error: response.should have_selector("li", :content => user.name)
NoMethodError:
undefined method name' for nil:NilClass
# ./spec/controllers/users_controller_spec.rb:39:inblock (5 levels) in '
# ./spec/controllers/users_controller_spec.rb:38:in each'
# ./spec/controllers/users_controller_spec.rb:38:inblock (4 levels) in '
I have tried a test to ensure that the user is returning an object instead of an array...it is. I have checked my sign_in and test_sign_in methods. I can probably move on, but fails tests drive me crazy.
Here is my users_controller_spec.rb code:
describe "for signed-in users" do
before(:each) do
#user = test_sign_in(Factory(:user))
second = Factory(:user, :name => "Bob", :email => "another#example.com")
third = Factory(:user, :name => "Ben", :email => "another#example.net")
#users = [#users, second, third]
end
it "should be successful" do
get :index
response.should be_success
end
it "should have the right title" do
get :index
response.should have_selector("title", :content => "All users")
end
it "should have an element for each user" do
get :index
#users.each do |user|
response.should have_selector("li", :content => user.name)
end
end
end
end
Please help!!!
I actually just solved this. In my code for # users = [#user, second, third] I misspelled #user with # users
Here is the rspec code for testing show in customers controller:
it "'show' should be successful" do
#category = Factory(:category)
#sales = Factory(:user)
#customer = Factory(:customer, :category1_id => category.id, :sales_id => sales.id)
category = mock_model('Category')
sales = mock_model('User')
customer = mock_model(Category, :sales_id => sales.id, :category1_id => category.id)
get 'show' , :id => customer.id
response.should be_success
end
Here is the error in rspec:
CustomersController GET customer page 'show' should be successful
Failure/Error: get 'show' , :id => customer.id
ActiveRecord::RecordNotFound:
Couldn't find Customer with id=1003
# c:in `find'
# ./app/controllers/customers_controller.rb:59:in `show'
# ./spec/controllers/customers_controller_spec.rb:50:in `block (3 levels) in <top (required)>'
The rspec test passes with the real record created by Factory (see #ed in rspec code)
What's wrong with the mock? Thanks.
The spec is failing inside the controller's action which doesn't know anything about your mocks unless you told it explicitly.
Add this to your spec, before the get statement.
Customer.should_receive(:find).and_return(customer)
I'm writing tests for my controller. They are very simple, but this error has kept popping up. This is my controller
def show
id=params[:id]
#user=User.find(:first,id)
end
My test
before(:each) do
#user = Fabricate(:user)
sign_in #user
end
...
it "should be successful" do
get "show", :id => #user
response.should be_success
end
And the error message
1) UsersController GET 'show' for the logged in user should be successful
Failure/Error: get "show", :id => #user
TypeError:
can't convert Symbol into Integer
# ./app/controllers/users_controller.rb:6:in `show'
# ./spec/controllers/users_controller_spec.rb:31:in `block (4 levels) in <top (required)>'
your controller is where the mistake is. The find method automatically only returns the first result (it is equivalent in code to User.where(:id => params[:id]).first). Try removing the :first symbol and simply pass in id (User.find(id))
get "show", :id => #user
Your problem here is likely with #user, whose value in the context of your spec is not clear from the example you've posted. You should be passing an integer record id as the value for the params argument to get, for example :id => 1.