2012-08-26 29 views
1

我是軌道和測試軌應用程序的新手。我嘗試用rspec測試現有的rails應用程序。
我剛剛完成模型測試,我也必須完成控制器測試。
但是我對rspec的sign_in方法有問題。我已經嘗試了互聯網上的所有解決方案,但仍然無法像使用rspec的用戶一樣登錄。
這是我的控制器代碼,它太簡單了;測試控制器與規格和工廠的女孩,不能sign_in

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 

這裏是我的規範代碼;

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 

這是我的工廠代碼;

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 

最後這裏是我的spec_helper代碼

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

這裏是錯誤;

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

解決方案必須是太容易了,但我在軌道上的新手,我無法找到它:(

回答

1

你必須讓it塊內的登錄步驟,以便向的RSpec能夠訪問的響應,如:

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

現在工作:D謝謝你。但爲什麼sign_in方法在塊之前不能工作? – user1609468

+0

其實我相信如果你把'sign_in @ customer'放到一個普通的'before'塊(而不是'before(:all)'塊)中,它也應該可以工作。 '之前(:全部)'在所有測試之前運行,所以我不認爲這個響應是在第一個測試(或其他任何其他測試)中進行的。 –

+0

另外,要清楚的是,如果你想檢查索引頁面是否正在加載,你可能想要在檢查響應之前添加一個'get:index'。正如它檢查的那樣,你的'sign_in @ customer'步驟的響應是成功的。 –

1

你有沒有受到

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 

幽祕ght需要visit customers_path

+0

你不需要定義一個主題來檢查響應是否成功。 –