1

The following rspec test file

require 'spec_helper'

describe EvaluationsController do
render_views

  before(:each) do
    association_attr
  end

  describe "'eval_selektor'" do

    before(:each) do
      @eval_selektor = get :eval_selektor, student_group_id: @student_group
    end

    it "should be successful" do
      @eval_selektor
      response.should be_success
    end

    ...

  end

  ...

end

is throwing the following error:

  1) EvaluationsController 'eval_selektor' should be successful
     Failure/Error: @eval_selektor = get :eval_selektor, student_group_id: @student_group
     NoMethodError:
       undefined method `student_groups' for nil:NilClass
     # ./app/controllers/application_controller.rb:7:in `get_student_group'
     # ./spec/controllers/evaluations_controller_spec.rb:14:in `block (3 levels) in <top (required)>'

from this method in the application_controller:

def get_student_group
  @user = current_user
  @student_group = @user.student_groups.find(params[:student_group_id])
end

At first I thought maybe rspec just wasn't getting passed the method from application_controller, but that's not the case as it can see it in the error. The code works in the browser, and if I put <%= @user %> in the view, it shows the correct user instance. Any ideas why rspec can't read @user?

dax
  • 10,779
  • 8
  • 51
  • 86
  • Because you have to stub current_user – apneadiving Aug 16 '13 at 11:30
  • @apneadiving, could you give me an example? I've been looking at [this thread](http://stackoverflow.com/questions/7448974/how-to-stub-applicationcontroller-method-in-request-spec) following your suggestion but can't get it to work. – dax Aug 16 '13 at 11:44

1 Answers1

1

apneadiving got me started in the right direction, and between this post and this one I got to the correct code:

  ApplicationController.any_instance.stub(:current_user).and_return(@user)
Community
  • 1
  • 1
dax
  • 10,779
  • 8
  • 51
  • 86