Arun Sharma
2010-Nov-12 09:56 UTC
[rspec-users] In spec/controller file not taking controller property
I am working on Rspec for Controller testing.I am initial leve of
rspec.I have a problem that in Spec/controller directory rspec file is
not taking controller property as (instance varibale,flash notice)
My controller code is as follow
class PortalNewsController < ApplicationController
def index
redirect_to root_path
end
def show
@portal_news = PortalNews.find(params[:id])
respond_to do |format|
format.html {render :action => ''show'', :layout =>
false}#
show.html.erb
format.xml { render :xml => @portal_news }
end
end
# GET /portal_news/new
# GET /portal_news/new.xml
def new
@portal_news = PortalNews.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @portal_news }
end
end
# GET /portal_news/1/edit
def edit
@portal_news = PortalNews.find(params[:id])
end
# POST /portal_news
# POST /portal_news.xml
def create
@portal_news = PortalNews.new(params[:portal_news])
puts "arundjfhdjfh"
respond_to do |format|
if @portal_news.save
flash[:notice] = ''PortalNews was successfully
created.''
format.html { redirect_to root_path }
format.xml { render :xml => @portal_news, :status => :created,
:location => @portal_news }
else
format.html { render :action => "new" }
format.xml { render :xml => @portal_news.errors, :status =>
:unprocessable_entity }
end
end
end
# PUT /portal_news/1
# PUT /portal_news/1.xml
def update
@portal_news = PortalNews.find(params[:id])
respond_to do |format|
if @portal_news.update_attributes(params[:portal_news])
flash[:notice] = ''PortalNews was successfully
updated.''
format.html { redirect_to root_path }
format.xml { head :ok }
else
flash[:notice] = ''PortalNews was not successfully
updated.''
format.html { render :action => "edit" }
format.xml { render :xml => @portal_news.errors, :status =>
:unprocessable_entity }
end
end
end
# DELETE /portal_news/1
# DELETE /portal_news/1.xml
def destroy
@portal_news = PortalNews.find(params[:id])
@portal_news.destroy
respond_to do |format|
format.html { redirect_to root_path }
format.xml { head :ok }
end
end
private
# Check for superadmin
def is_superadmin?
if !current_user.is_superadmin
flash[:error] = "Permission denied."
redirect_to root_path
end
end
end
And my rspec file code is
rspec/controllers/portal_news_controller_spec.rb
require File.dirname(__FILE__) +''/../spec_helper''
describe PortalNewsController do
integrate_views :true
controller_name :portal_news
it "should_be_redirect_to_root_path" do
get :index
response.should redirect_to(''/login'')
end
it "should_show " do
PortalNews.any_instance.stubs(:find).returns(@portal_news)
PortalNews.any_instance.stubs(:show).returns(true)
get :show, :id=>1
response.should render_template(''show'')
end
it "should_redirect_to_root_with_successfull_notice" do
post :create,:heading=>nil
assigns[:portal_news].errors.on(:heading).should_not be_nil
flash[:error].should_not be_nil
end
it "should_re-render_new_template_on_failed_save" do
PortalNews.any_instance.stubs(:valid?).returns(false)
post :create
assigns[:portal_news].should be_new_record
flash[:notice].should be_nil
response.should
render_template(''http://test.host/portal_news/new'')
end
it"should_pass_the_param_value" do
post :create,:portal_news => {:user_id => 11}
assigns[:portal_news].user_id.should =11
end
it "should_be update_on_valid_attributes" do
PortalNews.any_instance.stubs(:find).returns(@portal_news)
PortalNews.any_instance.stubs(:update_attributes).with(@portal_news).returns(true)
put :update, :id =>23
response.should redirect_to(''/login'')
flash[:notice].should_not be_nil
end
it "should_be update_on_invalid_attributes" do
PortalNews.any_instance.stubs(:find).returns(@portal_news)
PortalNews.any_instance.stubs(:update_attributes).with(@portal_news).returns(false)
put :update, :id =>23
response.should redirect_to(''/login'')
flash[:notice].should_not be_nil
end
it "should_check_variable" do
put :update,:id =>{:user_id =>11}
assigns[:id].user_id.should =11
end
end
And i am geeting error as follow
1 NoMethodError in ''PortalNewsController
should_redirect_to_root_with_successfull_notice''
You have a nil object when you didn''t expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.errors
2 NoMethodError in ''PortalNewsController
should_re-render_new_template_on_failed_save''
You have a nil object when you didn''t expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.new_record?
-----
Please post solution of this problem
3 NoMethodError in ''PortalNewsController
should_pass_the_param_value''
undefined method `user_id'' for nil:NilClass
--
Posted via http://www.ruby-forum.com/.
Arun Sharma
2010-Nov-12 11:13 UTC
[rspec-users] In spec/controller file not taking controller property
I am waiting for reply -- Posted via http://www.ruby-forum.com/.
David Chelimsky
2010-Nov-12 13:47 UTC
[rspec-users] In spec/controller file not taking controller property
On Nov 12, 2010, at 3:56 AM, Arun Sharma wrote:> I am working on Rspec for Controller testing.I am initial leve of > rspec.I have a problem that in Spec/controller directory rspec file is > not taking controller property as (instance varibale,flash notice) > My controller code is as follow > > class PortalNewsController < ApplicationController<snip/>> # POST /portal_news > # POST /portal_news.xml > def create > @portal_news = PortalNews.new(params[:portal_news]) > puts "arundjfhdjfh" > respond_to do |format| > if @portal_news.save > flash[:notice] = ''PortalNews was successfully created.'' > format.html { redirect_to root_path } > format.xml { render :xml => @portal_news, :status => :created, :location => @portal_news } > else > format.html { render :action => "new" } > format.xml { render :xml => @portal_news.errors, :status => :unprocessable_entity } > end > end > end<snip/>> private > > # Check for superadmin > def is_superadmin? > if !current_user.is_superadmin > flash[:error] = "Permission denied." > redirect_to root_path > end > endWhere is this ^^ method invoked? I don''t see any direct references to it, or a before_filter.> end > > And my rspec file code is > rspec/controllers/portal_news_controller_spec.rbThis ^^ should be "spec/controllers/...", not "rspec/controllers/...."> require File.dirname(__FILE__) +''/../spec_helper''If you''re on rspec-1.3 or greater, this ^^ can be shortened to require "spec_helper"> describe PortalNewsController do > integrate_views :true > controller_name :portal_newsYou don''t need this ^^ line (RSpec already knows the controller from "describe PortalNewsController"). <snip/>> it "should_redirect_to_root_with_successfull_notice" do > post :create,:heading=>nil > assigns[:portal_news].errors.on(:heading).should_not be_nil > flash[:error].should_not be_nil > end > > it "should_re-render_new_template_on_failed_save" do > PortalNews.any_instance.stubs(:valid?).returns(false) > post :create > assigns[:portal_news].should be_new_record > flash[:notice].should be_nil > response.should render_template(''http://test.host/portal_news/new'') > end > > it"should_pass_the_param_value" do > post :create,:portal_news => {:user_id => 11} > assigns[:portal_news].user_id.should =11 > end<snip/>> end > > And i am geeting error as follow > > 1 NoMethodError in ''PortalNewsController > should_redirect_to_root_with_successfull_notice'' > You have a nil object when you didn''t expect it! > You might have expected an instance of ActiveRecord::Base. > The error occurred while evaluating nil.errors > > 2 NoMethodError in ''PortalNewsController > should_re-render_new_template_on_failed_save'' > You have a nil object when you didn''t expect it! > You might have expected an instance of ActiveRecord::Base. > The error occurred while evaluating nil.new_record? > > 3 NoMethodError in ''PortalNewsController should_pass_the_param_value'' > undefined method `user_id'' for nil:NilClass > > ----- > Please post solution of this problemAll three errors are related to the create action, and all three suggest that we never get to the first line of the create action which assigns the new PortalNews to @portal_news. Any chance the is_super_admin? method is in a before_filter in ApplicationController? That''s the only thing I see in the code you posted that would lead to these errors. Everything else seems like it should work. HTH, David
Arun Sharma
2010-Nov-13 05:11 UTC
[rspec-users] In spec/controller file not taking controller property
thanks for reply>---> i commented all lines of code which you mentioned but still this code is notworking.Same code is working in other dummy application.> I have installed rails 2.3.5 and rspec 2.1.0 and rspec-rails 2.1.0 and I alsoinstalled plugin>----git://github.com/dchelimsky/rspec-rails.git -r ''refs/tags/1.2.9'' and git://github.com/dchelimsky/rspec.git -r ''refs/tags/1.2.9''>-----Please post the solution. I am waiting for reply -- Posted via http://www.ruby-forum.com/.
Arun Sharma
2010-Nov-13 06:59 UTC
[rspec-users] In spec/controller file not taking controller property
>My rspec file is not taking controller property.> In controller instance variable defined as :@portal_news=PortalNews.new> In rspec file my code isget :create,:portal_news=>{:user_id=>101} assigns[:portal_news].user_id.should=101> I getting error isNoMethodError in ''PortalNewsController should_check_variable'' undefined method `user_id'' for nil:NilClass> please post the solution.I am waiting for reply-- Posted via http://www.ruby-forum.com/.
David Chelimsky
2010-Nov-13 07:14 UTC
[rspec-users] In spec/controller file not taking controller property
On Nov 12, 2010, at 11:11 PM, Arun Sharma wrote:> thanks for reply >> --- > >> i commented all lines of code which you mentioned but still this code is not > working.Same code is working in other dummy application. > >> I have installed rails 2.3.5 and rspec 2.1.0 and rspec-rails 2.1.0 and I also > installed pluginThere''s your problem. RSpec-2 does not support Rails-2. Try using rspec-rails-1.3.3 and configuring gems (rather than plugins). HTH, David> >> ---- > git://github.com/dchelimsky/rspec-rails.git -r ''refs/tags/1.2.9'' > > and > > git://github.com/dchelimsky/rspec.git -r ''refs/tags/1.2.9'' > >> ----- > > Please post the solution. > > I am waiting for reply > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users