[rspec-users] Controller spec gives strange error

David Chelimsky dchelimsky at gmail.com
Thu Jul 3 03:44:19 EDT 2008


On Jul 3, 2008, at 2:24 AM, Damian Terentiev wrote:

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

Shouldn't this be cookies, not cookie? Doesn't explain why the test  
passes when the example fails, but it seems that both should be  
raising errors on this.

>
>  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/.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users



More information about the rspec-users mailing list