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.
Related
I'm trying to test the POST create action within my polymorphic comments controller. When running the spec below they fail with the error:
undefined method `comments' for nil:NilClass
Which I think means that #commentable isn't being created/set up properly, so it doesn't exist. ATM I am stubbing out the load_commentable method and returning the FactoryGirl question object, however this still doesn't seem to solve anything.
How can I amend my spec so that the commentable object is created properly and the comment is created within the scope of #commentable, as in the actual controller?
comments_controller.rb
def create
#comment = #commentable.comments.build(params[:comment])
#comment.user_id = current_user.id
respond_to do |format|
if #comment.save
format.html { redirect_to #commentable, notice: 'Comment created'}
format.js
else
format.html { redirect_to #commentable, notice: "Content can't be blank" }
format.js
end
end
end
def load_commentable
resource, id = request.path.split('/')[1,2]
#commentable = resource.singularize.classify.constantize.find(id)
end
comments_controller_spec.rb
describe CommentsController do
include Devise::TestHelpers
include AnswerHelper
before(:each) do
#user = create(:user)
#user2 = create(:user)
sign_in #user
sign_in #user2
#commentable = create(:question, user: #user2)
#comment = create(:comment, user: #user)
#vote = attributes_for(:vote, user: #user2, votable_id: #commentable)
controller.stub!(:load_commentable).and_return(#commentable)
controller.stub!(:current_user).and_return(#user)
#request.env['HTTP_REFERER'] = "http://test.host/questions/#{#commentable.id}"
stub_model_methods
end
describe "POST create" do
describe "with valid params" do
it "creates a new comment" do
expect {
post :create, comment: attributes_for(:comment), commentable: #commentable
}.to change(Comment, :count).by(1)
end
it "assigns a newly created comment as #comment" do
post :create, comment: attributes_for(:comment), commentable: #commentable
assigns(:comment).should be_a(Comment)
assigns(:comment).should be_persisted
end
end
describe "with invalid params" do
it "assigns a newly created but unsaved comment as #comment" do
Comment.any_instance.stub(:save).and_return(false)
post :create, comment: attributes_for(:comment), commentable: #commentable
assigns(:comment).should be_a_new(Comment)
end
end
end
factory.rb
factory :comment do
user
commentable_id :question
commentable_type "Question"
content "a comment"
votes_count 5
end
rspec resullts
1) CommentsController POST create with valid params creates a new comment
Failure/Error: post :create, comment: attributes_for(:comment), commentable: #commentable
NoMethodError:
undefined method `comments' for nil:NilClass
# ./app/controllers/comments_controller.rb:19:in `create'
# ./spec/controllers/comments_controller_spec.rb:24:in `block (5 levels) in <top (required)>'
# ./spec/controllers/comments_controller_spec.rb:23:in `block (4 levels) in <top (required)>'
The problem here is that you're stubbing out load_commentable, which is what is responsible for setting the instance variable #commentable in your controller. Since you're stubbing it, it never gets called, and the ivar never gets set - you can't set ivars in your controller from your rspec test suite directly.
Since you're creating a record, you don't actually need to stub anything, and can just pass the #commentable.id, then let it be looked up from the database. If you want to avoid the find for some reason, though, you could use:
Question.stub(:find).with(#commentable.id).and_return(#commentable)
This will cause your controller to use your #commentable object, and assign it to #commentable in the controller, at which point the test should continue to run normally.
I'm new to Rspec and fairly new to RoR. With that said, I've exhausted all my options trying to get this to work. I have a variable in a method to create a User in the create action of my UserController.rb. This variable gets the data from an authentication method. Then, I use this local variable, which is a response from an API call, to create the user according to the variables parameters. I've tried everything to my knowledge of Rspec, which is not much, without luck. I keep getting errors because the data variable is nil since I stub/mock the method and the variable.
If anyone could help me figure out how to test this or link me to a good tutorial (I've read a bunch) on how to do this, I would really appretiate it.
Here's my code:
users_controller.rb
def get_google_data
...
data = response.parsed #OAuth2
#id = data['id']
#email = data['email']
#fname = data['given_name']
#lname = data['family_name']
end
def create
get_google_data
puts "Got google data"
puts #id
if !#id.nil?
puts "data is not nil"
#user = User.find_by_google_id(#id)
puts #user
if #user.nil?
puts "inside user condition"
#user = User.new(:email => #email, :google_id => #id,
:first_name => #fname,
:last_name => #lname)
if #user.save
render json: #user, status: :created, location: #user
else
render json: #user.errors, status: :unprocessable_entity
end
else
puts "ended in the right Place"
render json: #user, location: #user
end
end
end
users_controller_spec.rb
describe "should not create duplicate user" do
it "returns user object that was previously created" do
#user = mock_model(User, :google_id=>1)
#controller.should_receive(:get_google_data).and_return(:true)
controller.instance_variable_set(:#id, 1)
User.stub!(:find_by_google_id).with(1).and_return(#user)
post :create
#user.should_not be_nil
end
end
I'm having 2 problems.
The test fails because I cannot control the data in data['id'] and the following assignments.
Failure/Error: post :create
NoMethodError:
undefined method `[]' for nil:NilClass
# ./app/controllers/users_controller.rb:109:in `get_google_data'
# ./app/controllers/users_controller.rb:118:in `create'
# ./spec/controllers/users_controller_spec.rb:6:in `block (3 levels) in <top (required)>'
The call to return #user gives me a circular reference error:
2) UsersController should not create duplicate user returns user object that was previously created
Failure/Error: post :create
ActiveSupport::JSON::Encoding::CircularReferenceError:
object references itself
# ./app/controllers/users_controller.rb:141:in `create'
# ./spec/controllers/users_controller_spec.rb:24:in `block (3 levels) in <top (required)>'
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.)
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