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

I am waiting for reply

On Nov 12, 2010, at 3:56 AM, Arun S. 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

 else
   format.html { render :action => "new" }
   format.xml  { render :xml => @portal_news.errors, :status => 

:unprocessable_entity }

 end

end
end

private

Check for superadmin

def is_superadmin?
if !current_user.is_superadmin
flash[:error] = “Permission denied.”
redirect_to root_path
end
end

Where 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.rb

This ^^ 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_news

You don’t need this ^^ line (RSpec already knows the controller from
“describe PortalNewsController”).

> 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

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 problem

All 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

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 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

thanks for reply

now this is working

On Nov 12, 2010, at 11:11 PM, Arun S. 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 plugin

There’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