[rspec-users] In spec/controller file not taking controller property

Arun Sharma lists at ruby-forum.com
Fri Nov 12 04:56:06 EST 2010


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


More information about the rspec-users mailing list