2
나는 before_action
필터를 가지고 있으며 사용자가 로그인 한 경우에만 색인 작업이 실행되는지 테스트하려고합니다. 간단히 말해서,이 작업을 수행하는 방법을 모르겠습니다. 내 자신의 간단한 인증을 사용하고 CanCan 또는 비슷한 사용할 수 있지만 내 자신의 학습을 위해 힘든 방법을하고있어!before_action 필터를 사용하여 RSPEC 테스트 색인 작업
ApplicationController.rb
helper_method :logged_in
helper_method :current_user
def current_user
@current_user ||= User.find_by_id(session[:current_user]) if session[:current_user]
end
def logged_in
unless current_user
redirect_to root_path
end
end
ActivitiesController.rb
before_action :logged_in
def index
@activities = Activity.all.where(user_id: @current_user)
end
Activities_Controller_spec.rb
require 'rails_helper'
RSpec.describe ActivitiesController, :type => :controller do
describe "GET index" do
before(:each) do
@activity = FactoryGirl.create(:activity)
session[:current_user] = @activity.user_id
@current_user = User.find_by_id(session[:current_user]) if session[:current_user]
end
it "shows all activities for signed in user" do
get :index, {user_id: @activity.user_id}
expect(response).to redirect_to user_activities_path
end
end
end
activities.rb (공장)
FactoryGirl.define do
factory :activity do
association :user
title { Faker::App.name }
activity_begin { Faker::Date.forward(10) }
activity_end { Faker::Date.forward(24) }
end
end
나는 다음과 같은 오류 받고 있어요 : 나는 테스트 (이 테스트되지 않은 상태입니다 :))과 같이 떨어지게해야한다고 생각 긴 토론 후
Failure/Error: expect(response).to redirect_to user_activities_path
Expected response to be a redirect to <http://test.host/users/1/activities> but was a redirect to <http://test.host/>.
Expected "http://test.host/users/1/activities" to be === "http://test.host/".
당신은 리디렉션을 테스트 할 수 있습니다 내가했습니다, – gotva
참으로 감사합니다 ([이] (http://www.relishapp.com/rspec/rspec-rails/docs/matchers/redirect-to-matcher)를 참조) 행위 리디렉션으로 변경되었지만 오류가 발생했습니다. 물론 – Robbo
이라는 질문을 업데이트 할 것입니다 :'response'와'user_activities_path' (URL 또는 그냥 문자열)를 비교하십시오. – gotva