Question

I'm relatively new to testing and very new to Rails 4 and rSpec. I am trying to test a controller that uses Devise for authentication and I am stuck. All of the examples I can find are for Rails 3.

I'm using Rails 4.0.3, Devise 3.2.3, rSpec 2.14.1 and FactoryGirl 4.4.0.

class LessonPlansController < ApplicationController
  before_action :authenticate_user!

  # GET /lesson_plans
  def index
    @lesson_plans = current_user.lesson_plans.to_a
  end

  .
  .
  .

private
  # Use callbacks to share common setup or constraints between actions.
  def set_lesson_plan
    @lesson_plan = LessonPlan.find(params[:id])
  end

  # Only allow a trusted parameter "white list" through.
  def lesson_plan_params
    params[:lesson_plan]
  end

  def lesson_plan_params
    params.require(:lesson_plan).permit(:title, :synopsis)
  end
end

Here are my factory definitions: (Maybe I don't need to define user_id in the lesson_plan factory?)

FactoryGirl.define do
    factory :user do
    sequence( :username )  { |n| "user#{n}" }
    sequence( :email )     { |n| "foo#{n}@example.com" }
    password               'foobarbaz'
    password_confirmation  'foobarbaz'
    created_at             Time.now
    updated_at             Time.now
  end
end

FactoryGirl.define do
  factory :lesson_plan do
    user_id 1
    title    "The French Revolution"
    synopsis "Background and events leading up to the French Revolution"
  end
end

And the test part is where I get stuck.

describe LessonPlansController do
  let(:valid_attributes) { {  } }
  let(:valid_session) { {} }

  # describe "GET index" do
  it "assigns all lesson_plans as @lesson_plans" do
    user=FactoryGirl.create(:user)
    sign_in user
    lesson_plan = LessonPlan.create! valid_attributes
    get :index, {}, valid_session
    assigns(:lesson_plans).should eq([lesson_plan])
  end
end

I'm not sure what to put in valid_attributes and valid_session (or if I even need them). The test will get as far as signing in the user, but will fail on creation of the lesson_plan. Admittedly this is the default/generated test for rSpec, but I am not sure how to proceed.

Examples I have seen use a before block to set up the user. I haven't been able to find anything on the Devise wiki page covering how to write basic rSpec tests for a controller that requires the user to be logged in. Any pointers would be greatly appreciated!

Was it helpful?

Solution

"I'm not sure what to put in valid_attributes and valid_session (or if I even need them)."

Well that depends what you're testing for.. Say you're testing validations & want to ensure that a record not be created if x column is set to null... then you could try to specifically create a record with invalid attributes (e.g. column: nil) and expect the result to not return true; maybe you want to ensure that it IS created with valid attributes.

You can btw, use `attributes_for(:factory_name)`` since you're using FactoryGirl. And no you don't necessarily need to specify the user's id in your lesson plan factory; unless you always want it to reference user 1. You can simply reference user with no value. Check out http://everydayrails.com/2012/03/12/testing-series-intro.html and especially parts 3-5 for an introduction to testing with RSPec.. I found this a pretty easy to follow guide when I was getting started.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top