gpt4 book ai didi

ruby-on-rails-4 - Rails,Devise,Rspec:未定义的方法 'sign_in'

转载 作者:行者123 更新时间:2023-12-03 09:07:13 26 4
gpt4 key购买 nike

我正在尝试使用Devise辅助方法登录和注销在Rails中编写Rspec测试。 sign_in方法不起作用。但是,在对应用程序进行大量更改之前,它可以更早地工作。

我尝试过的事情:

  • 我将测试助手包括在Rspec.configure中。
  • 使用Warden的login_as
  • 清除Rails缓存。
  • 摆脱 capybara ,看看是否引起了
  • 问题
  • 我未在 Controller 规格中明确设置 session (例如,没有有效的 session )

  • 到目前为止,还没有骰子。与登录用户一起测试我的 Controller 时,我需要做些什么?

    错误信息:
     OrderItemsController GET #index renders the :index view
    Failure/Error: sign_in :admin
    NoMethodError:
    undefined method `sign_in' for # <RSpec::ExampleGroups::OrderItemsController_2::GETIndex:0x00000102c002d0>
    # ./spec/controllers/order_items_controller_spec.rb:6:in `block (2 levels) in <top (required)>'

    Controller 规格
    require 'spec_helper'

    describe OrderItemsController do
    before (:each) do
    admin = create(:admin)
    sign_in :admin
    end

    describe "GET #index" do
    it "renders the :index view" do
    get :index
    expect( response ).to render_template :index
    end
    end
    end

    spec_helper.rb
    require 'rspec/rails'
    require 'capybara/rspec'

    RSpec.configure do |config|

    config.include ApplicationHelper
    config.include ControllersHelper
    config.include UsersHelper
    config.include Devise::TestHelpers, type: :controller
    config.include FactoryGirl::Syntax::Methods

    end

    gem 文件
    group :development, :test do
    gem 'rspec-rails', '~> 3.0.0.beta'
    gem 'capybara'
    gem 'factory_girl_rails'
    gem 'faker'
    gem 'dotenv-rails'
    gem 'guard'
    gem 'guard-annotate'
    gem 'guard-rspec', require: false
    gem 'guard-livereload', require: false
    gem 'foreman'
    end

    factory / user.rb
    FactoryGirl.define do

    factory :user do
    first { Faker::Name.first_name }
    last { Faker::Name.last_name }
    email { Faker::Internet.email }
    admin false
    password "secrets1"
    password_confirmation "secrets1"
    confirmed_at Date.today

    factory :admin do
    admin true
    end
    end
    end

    提前致谢。

    最佳答案

    您最近是否像我一样升级到RSpec 3?这是从the RSpec 3 documentation:

    Automatically Adding Metadata RSpec versions before 3.0.0 automatically added metadata to specs based on their location on the filesystem. This was both confusing to new users and not desirable for some veteran users.

    In RSpec 3, this behavior must be explicitly enabled:

    ​# spec/rails_helper.rb
    RSpec.configure do |config|
    config.infer_spec_type_from_file_location!
    end

    Since this assumed behavior is so prevalent in tutorials, the default configuration generated by rails generate rspec:install enables this.

    If you follow the above listed canonical directory structure and have configured infer_spec_type_from_file_location!, RSpec will automatically include the correct support functions for each type.



    添加该配置代码段后,我不再需要指定规范类型(例如 type: :controller)。

    关于ruby-on-rails-4 - Rails,Devise,Rspec:未定义的方法 'sign_in',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23859653/

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