Here is the error for update in rspec:
4) CustomersController GET customer page 'update' should be successful
Failure/Error: post 'update', customer
NoMethodError:
undefined method `symbolize_keys' for "1":String
# ./spec/controllers/customers_controller_spec.rb:38:in `block (3 levels) in <top (required)>'
The rspec code:
it "'update' should be successful" do
customer = Factory(:customer)
post 'update', customer
response.should be_success
end
The update in customers controller:
def update
#customer = Customer.find(params[:id])
if #customer.update_attributes(params[:customer], :as => :roles_new_update)
if #customer.changed
#message = 'The following info have been changed\n' + #customer.changes.to_s
#subject ='Customer info was changed BY' + session[:user_name]
notify_all_in_sales_eng(#message,#subject)
end
redirect_to session[('page'+session[:page_step].to_s).to_sym], :notice => 'Customer was updated successfaully!'
else
render 'edit', :notice => 'Customer was not updated!'
end
end
Any thoughts about the error? Thanks.
I will not enter into details on RSpec, but I just met the same error and this is how I would correct it for your code:
it "'update' should be successful" do
customer = Factory(:customer)
post 'update', :id => customer.id
response.should be_success
end
I think you can't provide your object directly to the post method, you must pass it's id as in a Hash instead.
(Please note too that this code assumes your customer exists in the test database, so your Factory must create it.)
Related
I run an RSpec testcase that fills a form and submits it. I get the following error:
1) Sign Up Advertiser after adding valid information should create a user
Failure/Error: expect { click_button submit }.to change(User, :user_key)
NoMethodError:
undefined method `model_name' for Fixnum:Class
# /mnt/hgfs/Projekte/adserve.example.de/app/controllers/advertisers_controller.rb:31:in `tryToCreateUser'
# /mnt/hgfs/Projekte/adserve.example.de/app/controllers/advertisers_controller.rb:14:in `create'
# ./sign_up_advertiser_spec.rb:32:in `block (4 levels) in <top (required)>'
# ./sign_up_advertiser_spec.rb:32:in `block (3 levels) in <top (required)>'
This is the code for the controller:
class AdvertisersController < ApplicationController
...
def home
#menuindex = 0
end
def create
#user = Advertiser.new (params[:advertiser])
tryToCreateUser
end
def tryToCreateUser
if #user.save
#user = Advertiser.retrieve(#user.id)
redirect_to home, :notice => "You successfully signed up " + #user.full_name
else
render :action => "/users/new", :layout => 'application'
end
end
end
And this is what the routes.rb looks like
match "signup_advertiser" => "advertisers#new", :as => "signup_advertiser"
match "signup_publisher" => "publishers#new", :as => "signup_publisher"
get "advertisers_home" => "advertisers#home"
resources :advertisers
So I guess the mistake is in the redirect_to part. But I can't figure it out. I fiddled around with rendering a custom action in 'home' and some other stuff. I think it's something pretty basic so help would be very appreciated. Thanks.
Yes, the problem is there. You should use home as a symbol on the redirect_to method:
def tryToCreateUser
if #user.save
#user = Advertiser.retrieve(#user.id)
redirect_to :home, :notice => "You successfully signed up " + #user.full_name
else
render :action => "/users/new", :layout => 'application'
end
end
What you do now is: redirect_to 0 since you are actually calling the controllers method "home".
Here is the rspec code for create in controller:
describe "'create' successful" do
before(:each) do
#customer = mock_model(Customer)
#customer.stub(:save).and_return(true)
session[:sales] = true
session[:user_id] = 1
session[:user_name] = "sales.name"
session[:page_step] = 1
session['page1'] = customers_path
end
it "should create one customer record" do
lambda do
put 'create', #customer
end.should change(Customer, :count).by(1)
end
it "should redirect to customers path" do
put 'create', #customer
flash[:notice].should_not be_nil
response.should redirect_to(customers_path)
end
end
Here is the create in controller:
def create
if session[:sales]
#customer = Customer.new(params[:customer], :as => :roles_new_update)
#customer.sales_id = session[:user_id]
if #customer.save
#message = "New customer #{params[:name]} was created. Please check it out"
#subject = "New customer #{params[:name]} was created BY {#session[:user_name]}"
UserMailer.notify_tl_dh_ch_ceo(#message, #subject, session[:user_id])
redirect_to session[('page' + session[:page_step].to_s).to_sym], :notice => 'Customer was created successfaully!'
else
render 'new', :notice => 'Customer was not saved!'
end
end
end
Here is the error in rspec:
1) CustomersController GET customer page 'create' successful should create one customer record
Failure/Error: put 'create', #customer
NoMethodError:
undefined method `symbolize_keys' for "1001":String
# ./spec/controllers/customers_controller_spec.rb:40:in `block (5 levels) in <top (required)>'
# ./spec/controllers/customers_controller_spec.rb:39:in `block (4 levels) in <top (required)>'
2) CustomersController GET customer page 'create' successful should redirect to customers path
Failure/Error: put 'create', #customer
NoMethodError:
undefined method `symbolize_keys' for "1002":String
# ./spec/controllers/customers_controller_spec.rb:45:i
The problem is with the mocking data of #customer. What's wrong with the mocking in spec?
Thanks.
There is nothing wrong, you simply have to make your model fetch the mock you've created.
Proceed this way:
#customer = mock_model(Customer)
#customer.stub(:save).and_return(true)
Customer.should_receive(:new).and_return(#customer)
Rereading your question I figured out that Rails could be complaining because when you pass a variable to your route, it should respond to to_param.
But your mock doesn't: it's nothing.
I'd say choose among the following:
use a Factory which is a real ActiveRecord object with the right properties
edit your test for something like put 'create', 1234 or any integer you want.
BTW, put triggers the update action which is not supposed to created objects but simply change them.
Here is the error in rspec:
CategoriesController GET 'update' should be successful
Failure/Error: get 'update'
ActiveRecord::RecordNotFound:
Couldn't find Category without an ID
# c:in `find'
# ./app/controllers/categories_controller.rb:45:in `update'
# ./spec/controllers/categories_controller_spec.rb:35:in `block (3 levels) in <top (required)>'
Here is the code in controller:
def edit
#category = Category.find(params[:id])
end
def update
#category = Category.find(params[:id])
##category.reload. caused nil.reload error
if #category.update_attributes(params[:category], :as => :roles_update)
#category = Category.find(params[:id])
redirect_to #category, :notice => 'Category was successfully updated'
else
#categories = Category.all
render 'index'
end
end
Here is the rspec code:
describe "GET 'update'" do
it "should be successful" do
get 'update'
response.should be_success
end
end
Any thoughts? Thanks.
You pasted the create action instead of the update action. Also, you are trying to test the update action with a get request.. it should be with a put request if you are following the conventions.
If you had, say, the update action implemented... you would test more or less like:
describe CategoriesController do
let(:category) { mock_model(Category).as_null_object }
describe "PUT update" do
before do
Category.should_receive(:find).with(5).and_return(category)
end
context "when a category updates succesfully" do
before do
category.stub(:update_attributes).and_return(true)
end
it "redirects to the categories page" do
put :update, :id => 5, :category => { :some_val => 4 }
response.should redirect_to(categories_path)
end
it "sets the flash message" do
put :update, :id => 5, :category => { :some_val => 4 }
flash[:notice].should eq("Category was succesfully updated")
end
end
context "when a category does not update successfully" do
before do
category.stub(:update_attributes).and_return(false)
end
it "sets the flash message"
it "redirects to the categories page"
# etc etc
end
end
end
To get to this point (meaning the addition of mock models, stubs, what have you) you would normally start "fresh" so to speak and work your way up TDD style. Hope it helps
I'm working on the exercises from Chapter 10 of the Rails Tutorial and ran in to a snag with the exercise that has me ensure that an admin user can't delete themselves. My initial idea was to simply check the id of the current user and compare it against params[:id] to make sure that they're not equal. My destroy action in my Users controller looked like this:
def destroy
if current_user.id == params[:id].to_i
flash[:notice] = "You cannot delete yourself."
else
User.find(params[:id]).destroy
flash[:success] = "User destroyed."
end
redirect_to users_path
end
This works perfectly when I test it manually in the app but 3 of my RSpec tests fail with the same "undefined method 'to_i'" error (as seen below):
1) UsersController DELETE 'destroy' as an admin user should destory the user
Failure/Error: delete :destroy, :id => #user
NoMethodError:
undefined method `to_i' for #<User:0x000001032de188>
# ./app/controllers/users_controller.rb:48:in `destroy'
# ./spec/controllers/users_controller_spec.rb:310:in `block (5 levels) in <top (required)>'
# ./spec/controllers/users_controller_spec.rb:309:in `block (4 levels) in <top (required)>'
2) UsersController DELETE 'destroy' as an admin user should redirect to the users page
Failure/Error: delete :destroy, :id => #user
NoMethodError:
undefined method `to_i' for #<User:0x000001032b5850>
# ./app/controllers/users_controller.rb:48:in `destroy'
# ./spec/controllers/users_controller_spec.rb:315:in `block (4 levels) in <top (required)>'
3) UsersController DELETE 'destroy' as an admin user should not allow you to destroy self
Failure/Error: delete :destroy, :id => #admin
NoMethodError:
undefined method `to_i' for #<User:0x0000010327e350>
# ./app/controllers/users_controller.rb:48:in `destroy'
# ./spec/controllers/users_controller_spec.rb:321:in `block (5 levels) in <top (required)>'
# ./spec/controllers/users_controller_spec.rb:320:in `block (4 levels) in <top (required)>'
If I use the params[:id] to find the user and compare it to the current_user like I have below then it works both in the app and in RSpec.
def destroy
if current_user == User.find(params[:id])
flash[:notice] = "You cannot delete yourself."
else
User.find(params[:id]).destroy
flash[:success] = "User destroyed."
end
redirect_to users_path
end
Why would there be a problem in RSpec with the "to_i" method? If anyone is wondering I was leaning toward that approach because I thought it would best to simply compare the current user id to the id of the user targeted for deletion (via the params[:id]) instead of hitting the db to "find" the user.
For reference this is my RSpec test:
describe "DELETE 'destroy'" do
before(:each) do
#user = Factory(:user)
end
...
describe "as an admin user" do
before(:each) do
#admin = Factory(:user, :email => "admin#example.com", :admin => true)
test_sign_in(#admin)
end
it "should destory the user" do
lambda do
delete :destroy, :id => #user
end.should change(User, :count).by(-1)
end
it "should redirect to the users page" do
delete :destroy, :id => #user
response.should redirect_to(users_path)
end
it "should not allow you to destroy self" do
lambda do
delete :destroy, :id => #admin
end.should change(User, :count).by(0)
response.should redirect_to(users_path)
flash[:notice].should =~ /cannot delete yourself/
end
end
end
Any help would be appreciated!
In your specs, try using #user.id instead of #user on your :id parameter (I realize the Tutorial says to just use #user, but something may be going on where the id isn't being properly extracted):
delete :destroy, :id => #user.id
But you may consider restructuring to something like this:
#user = User.find(params[:id])
if current_user == #user
flash[:notice] = "You cannot delete yourself."
else
#user.destroy
flash[:success] = "User destroyed."
end
I'm running through the Ruby on Rails Tutorial 3, and having a ball doing it, but I've just come across some problems that aren't getting solved. When I run my specs, two tests fail.
Failures:
1) UsersController PUT 'update' failure should render the 'edit' page
Failure/Error: put :update, :id => #user, :user => #attr
undefined local variable or method `object' for #<#<Class:0x00000102c861c8>:0x00000101d25558>
# ./app/views/shared/_error_messages.html.erb:3:in `_app_views_shared__error_messages_html_erb___3390867530789228804_2170854120__2806434579894406668'
# ./app/views/users/edit.html.erb:4:in `block in _app_views_users_edit_html_erb__558009768664311469_2170714160__919273585470661416'
# ./app/views/users/edit.html.erb:3:in `_app_views_users_edit_html_erb__558009768664311469_2170714160__919273585470661416'
# ./app/controllers/users_controller.rb:47:in `update'
# ./spec/controllers/users_controller_spec.rb:158:in `block (4 levels) in <top (required)>'
2) UsersController PUT 'update' failure should have the right title
Failure/Error: put :update, :id => #user, :user => #attr
undefined local variable or method `object' for #<#<Class:0x00000102c861c8>:0x00000101b211f8>
# ./app/views/shared/_error_messages.html.erb:3:in `_app_views_shared__error_messages_html_erb___3390867530789228804_2170854120__2806434579894406668'
# ./app/views/users/edit.html.erb:4:in `block in _app_views_users_edit_html_erb__558009768664311469_2170714160__919273585470661416'
# ./app/views/users/edit.html.erb:3:in `_app_views_users_edit_html_erb__558009768664311469_2170714160__919273585470661416'
# ./app/controllers/users_controller.rb:47:in `update'
# ./spec/controllers/users_controller_spec.rb:163:in `block (4 levels) in <top (required)>'
I've searched through my code as best I can comparing it against the code in the book, and I've come up with nothing. I'm sure it's one stupid little thing that I've missed, and I would greatly appreciate a second pair (or more ;) of eyes.
Here are my tests:
describe "failure" do
before(:each) do
#attr = { :email => "", :name => "", :password => "", :password_confirmation => "" }
end
it "should render the 'edit' page" do
put :update, :id => #user, :user => #attr
response.should render_template('edit')
end
it "should have the right title" do
put :update, :id => #user, :user => #attr
response.should have_selector("title", :content => "Edit User")
end
end
And here is the update methods from the users_controller:
def update
#user = User.find(params[:id])
if #user.update_attributes(params[:user])
flash[:success] = "Profile updated"
redirect_to #user
else
#title = "Edit User"
render 'edit'
end
end
Any thoughts on where I should look are greatly appreciated.
The source of the problem is not in spec files even though the problem manifests itself in those spec files. The instructor, Michael Hartl, changes the following statement:
<%= render 'shared/error_messages' %
to
<%= render 'shared/error_messages', :object => f.object %>
statement. In other words, he adds “, :object => f.object” to the first statement. And you must look in all files that have the original statement and change them to the second one. If you miss any of them you will have these errors. Specifically look in the following files (and any other that may have the original statement):
app/views/users/edit.html.erb
app/views/users/fields.html.erb
app/views/users/new.html.erb
app/views/shared/micropost_form.html.erb