我使用的是authlogic,想测试删除控制器的current_user或会话的user_authentication。理想情况下,我希望此规格通过取消控制器的会话或current_user(已包含尝试作为注释)来传递(302状态为登录页面):
describe "GET #new" do
it "returns http success" do
get :new
expect(response).to have_http_status(:success)
# session = nil
# binding.pry
# UserSession.find.destroy
UserSession.find.destroy
# @request.session = {}
# controller.session = nil
# @request.session["user_credentials"] = nil
# request.env["rack.session"] = nil
get :new
expect(response).to have_http_status(302)
end
end
以及在rspec_helper.rb
中# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
require 'authlogic/test_case'
RSpec.configure do |config|
config.include Authlogic::TestCase, type: :controller
config.before(:example, type: "controller") do
activate_authlogic
end