gpt4 book ai didi

ruby-on-rails - 在 Rspec 测试中使用 Devise 进行身份验证

转载 作者:行者123 更新时间:2023-12-03 21:40:03 25 4
gpt4 key购买 nike

我无法让我的 Rspec 测试在 Devise 尝试在 before_filter 中对用户进行身份验证的项目上正确运行。

我遵循了 Devise Git wiki 中的示例:How To: Controllers and Views tests with Rails 3 (and rspec)

规范/spec_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__)
require 'rspec/rails'
require 'rspec/autorun'

Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
config.include Devise::TestHelpers, :type => :controller
config.extend ControllerMacros, :type => :controller
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_base_class_for_anonymous_controllers = false
end

规范/支持/controller_macros.rb:
 module ControllerMacros
def login_user
before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:user]
user = FactoryGirl.create(:user)
sign_in user
end
end
end

规范/ Controller /test.rb:
require 'spec_helper'
describe ProjectsController do
def valid_attributes
{
:name => 'Project',
:description => 'Description'
}
end

def valid_session
{}
end

describe "Test" do

login_user

it "assigns all projects as @projects" do
# test passes if the following line is here, otherwise fails
subject.current_user.should_not be_nil

project = Project.create! valid_attributes
get :index, {}, valid_session
assigns(:projects).should eq([project])
end
end
end

app/controllers/projects_controller.rb (直接来自生成器,除了添加前过滤器:
class ProjectsController < ApplicationController

before_filter :authenticate_user!

# GET /projects
# GET /projects.json
def index
@projects = Project.all

respond_to do |format|
format.html # index.html.erb
format.json { render json: @projects }
end
end

# GET /projects/1
# GET /projects/1.json
def show
@project = Project.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.json { render json: @project }
end
end

# GET /projects/new
# GET /projects/new.json
def new
@project = Project.new

respond_to do |format|
format.html # new.html.erb
format.json { render json: @project }
end
end

# GET /projects/1/edit
def edit
@project = Project.find(params[:id])
end

# POST /projects
# POST /projects.json
def create
@project = Project.new(params[:project])

respond_to do |format|
if @project.save
format.html { redirect_to @project, notice: 'Project was successfully created.' }
format.json { render json: @project, status: :created, location: @project }
else
format.html { render action: "new" }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end

# PUT /projects/1
# PUT /projects/1.json
def update
@project = Project.find(params[:id])

respond_to do |format|
if @project.update_attributes(params[:project])
format.html { redirect_to @project, notice: 'Project was successfully updated.' }
format.json { head :ok }
else
format.html { render action: "edit" }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end

# DELETE /projects/1
# DELETE /projects/1.json
def destroy
@project = Project.find(params[:id])
@project.destroy

respond_to do |format|
format.html { redirect_to projects_url }
format.json { head :ok }
end
end
end

为什么包含 subject.current_user.should_not be_nil 的测试通过但不包括在内时失败?

最佳答案

我在关注 the how-to you mention 时遇到了同样的问题.解决方案是删除对 valid_session 的调用。来自每个请求,因为它覆盖了 sign_in 设置的 session 值 helper 。

它实际上以相同的方法记录在代码块中,我也错过了:(

关于ruby-on-rails - 在 Rspec 测试中使用 Devise 进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11002721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com