user1609468
user1609468

Reputation: 113

testing controller with spec and factory girl, can't sign_in

I'm newbie on rails and testing rails applications. I try to test an existing rails application with rspec.
I've just finished model tests and i have to complete controller tests too.
But i have a problem with sign_in method on rspec. I've tried all solution methods on the internet but still i can't sign in like a user with rspec.
here is the my controller code, it's too simple;

class AboutController < ApplicationController
  def index
  end

  def how_it_works
  end

  def what_is
  end

  def creative_tips
  end

  def brand_tips
  end

  def we_are
  end

  def faq
  end
end

and here is the my spec code;

require 'spec_helper'

describe AboutController do

  before(:all) do
    @customer=Factory.create(:customer)
    sign_in @customer
  end

  context 'index page :' do

    it 'should be loaded successfully' do
      response.should be_success
    end

  end

end

Here is the my factory code;

Factory.define :unconfirmed_user, :class => User do |u|
  u.sequence(:user_name) {|n| "user_#{n}"}
  u.sequence(:email){|n| "user__#{n}@example.com"}
  u.sequence(:confirmation_token){|n| "confirm_#{n}"}
  u.sequence(:reset_password_token){|n| "password_#{n}"}
  u.password '123456'
  u.password_confirmation '123456'
  u.user_type :nil
end

Factory.define :user, :parent => :unconfirmed_user do |u|
  u.confirmed_at '1-1-2010'
  u.confirmation_sent_at '1-1-2010'
end

Factory.define :customer, :parent => :user do |u|
  u.user_type :customer
end

Finally here is the my spec_helper code

ENV["RAILS_ENV"] ||= 'test'
require File.dirname(__FILE__) + "/../config/environment" unless defined?(Rails)
require 'rspec/rails'
require "paperclip/matchers"

Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}

RSpec.configure do |config|
  config.include Paperclip::Shoulda::Matchers
end

RSpec.configure do |config|
  config.include Devise::TestHelpers, :type => :controller
end

RSpec.configure do |config|
  config.mock_with :rspec
  config.fixture_path = "#{::Rails.root}/spec/fixtures"
  config.use_transactional_fixtures = true
end

gem file;

gem 'rails', '3.0.3'
gem 'mysql2', '< 0.3'
.
.
.
gem "devise" , "1.1.5"
.
.
.

group :test do
  gem 'shoulda'
  gem "rspec-rails", "~> 2.11.0"
  gem "factory_girl_rails"
end

and here is the error;

Failures:

  1) AboutController index page : should be loaded successfully
     Failure/Error: sign_in @customer
     NoMethodError:
       undefined method `env' for nil:NilClass
     # ./spec/controllers/about_controller_spec.rb:7:in `block (2 levels) in <top (required)>'

solution must be too easy but I'm newbie on rails and i can't find it :(

Upvotes: 1

Views: 1135

Answers (2)

Michael Durrant
Michael Durrant

Reputation: 96484

You have no subject.

require 'spec_helper'

describe AboutController do

  before(:all) do
    @customer=Factory.create(:customer)
    sign_in @customer
  end

  context 'index page :' do

    subject { Page }    
    it 'should be loaded successfully' do
      response.should be_success
    end

  end

end

You might need to visit customers_path.

Upvotes: 1

Chris Salzberg
Chris Salzberg

Reputation: 27374

You have to make the sign-in step inside the it block in order for rspec to be able to access the response, e.g.:

it 'should be loaded successfully' do
  sign_in @customer
  response.should be_success
end

Upvotes: 1

Related Questions