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

David Chelimsky dchelimsky at gmail.com
Fri Nov 12 08:47:28 EST 2010


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
>  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").

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


More information about the rspec-users mailing list