저는 레일 및 테스트 레일 애플리케이션의 초보자입니다. rspec을 사용하여 기존 레일 애플리케이션을 테스트하려고합니다.
방금 모델 테스트를 마쳤으며 컨트롤러 테스트도 완료해야합니다.
하지만 rspec에 sign_in 메서드에 문제가 있습니다. 인터넷에서 모든 솔루션 방법을 시도했지만 여전히 rspec 사용자와 같이 로그인 할 수 없습니다.
여기 내 컨트롤러 코드입니다. 너무 간단합니다.사양 및 컨트롤러가있는 테스트 컨트롤러, 로그인 할 수 없습니다.
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)>'
솔루션은 너무 쉽게해야하지만 레일 초보자 그리고 난 그것을 찾을 수 없습니다 :(
이제 작동 : D 감사합니다. 하지만 왜 sign_in 메소드가 블록 이전에 작동하지 않는 걸까요? – user1609468
실제로'sign_in @ customer '를 일반적인'before' 블록 ('before (: all)'블록이 아닌)에 넣는다면 실제로 작동해야한다고 생각합니다. 'before (: all)'은 모든 테스트 전에 실행되기 때문에 응답이 첫 번째 테스트 (또는 그 문제에 대해서는 다른 테스트)로 이어지지 않는다고 생각합니다. –
또한 인덱스 페이지가로드되는지 확인하려면 응답을 확인하기 전에'get : index'를 추가하는 것이 좋습니다. 그것이 검사하는 것과 마찬가지로'sign_in @ customer' 단계의 응답이 성공적이라는 것입니다. –