[rspec-users] Controller spec gives strange error

Damian Terentiev lists at ruby-forum.com
Thu Jul 3 03:24:15 EDT 2008


Good day!

I have a Rails app with a controller that sets an instance variable in a
before_filter, like this:

class PostsController < ApplicationController

  before_filter :set_site_language

  def set_site_language
    @site_language = cookie['lang'] ?
      Language.find_by_code(cookie['lang']) :
      Language.find(:first)
  end

  def index
    @posts = Post.find_by_language_id(@site_language.id)
  end

end

The controller has the following spec:

describe PostsController do

  it "should show home page" do
    get :index
    response.should be_success
  end

end

And the equivalent test:

class PostsControllerTest < ActionController::TestCase

  def test_should_show_home_page
    get :index
    assert_response :success
  end

end

The test runs fine, but the spec fails with the following error on line
with `@posts = Post.find_by_language_id(@site_language.id)`:

RuntimeError in 'PostsController should show home page'
Called id for nil, which would mistakenly be 4 -- if you really wanted
the id of nil, use object_id

Seems that @site_language does not get assigned when spec runs. Could
you please help me solve this problem?

Yours,
Damian
-- 
Posted via http://www.ruby-forum.com/.


More information about the rspec-users mailing list