Ruby on rails Rspec登录\用户问题
我在使用designe/Rspec登录用户以访问测试路径时遇到问题。我使用的是Deviate概述的Ruby on rails Rspec登录\用户问题,ruby-on-rails,rspec,devise,Ruby On Rails,Rspec,Devise,我在使用designe/Rspec登录用户以访问测试路径时遇到问题。我使用的是Deviate概述的support/controller\u宏模块,但每当我尝试使用 login\u user在测试的任何部分,我都会遇到错误:before在示例(例如it块)内或在示例范围内运行的构造(例如before、let等)中不可用。它仅在示例组(例如描述或上下文块)上可用。 我试过移动东西,确保我所有的要求都设置正确,等等 我的测试: require "rails_helper" RSpec.descri
support/controller\u宏
模块,但每当我尝试使用
login\u user
在测试的任何部分,我都会遇到错误:before在示例(例如it块)内或在示例范围内运行的构造(例如before、let等)中不可用。它仅在示例组(例如描述或上下文块)上可用。
我试过移动东西,确保我所有的要求都设置正确,等等
我的测试:
require "rails_helper"
RSpec.describe BallotsController, type: :controller do
describe "index" do
it "renders" do
login_user
ballots_path
expect(response).to be_success
expect(response).to render_template("index")
end
end
end
(我已经尝试在descripe块和上面的块中添加login\u user)
我的控制器\u宏:
def login_user
before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:user_confirmed]
user = FactoryBot.create(:user_confirmed)
sign_in user
end
end
def login_admin
before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:admin]
user = FactoryBot.create(:admin)
sign_in user
end
end
end
我的等级库助手:
require "rails_helper"
require_relative "support/controller_macros"
RSpec.configure do |config|
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
config.shared_context_metadata_behavior = :apply_to_host_groups
config.include ControllerMacros, :type => :controller
config.include Devise::Test::ControllerHelpers, :type => :controller
config.example_status_persistence_file_path = "spec/examples.txt"
config.disable_monkey_patching!
if config.files_to_run.one?
config.default_formatter = "doc"
end
config.profile_examples = 10
config.order = :random
Kernel.srand config.seed
end
我希望它能让用户登录,并让控制器正确地点击索引路由。谢谢大家! 代码中唯一的问题(为了解决错误,不深入讨论是否应该使用此方法)是您在it
块中有登录用户,这不能是因为它在(:each)之前调用了
如果您看到,您还将看到它在it块中没有此调用,而是在描述块中的it块之外。它将此调用应用于该描述块中的所有it块
您的代码将命名为:
RSpec.describe BallotsController, type: :controller do
describe "index" do
login_user
it "renders" do
ballots_path
expect(response).to be_success
expect(response).to render_template("index")
end
end
end
我喜欢的方式:
在控制器\u宏中,将登录\u用户替换为:
def login_user(user)
@request.env["devise.mapping"] = Devise.mappings[:user_confirmed]
sign_in user
end
现在,无论您想在哪里登录用户,都可以执行以下操作:
RSpec.describe BallotsController, type: :controller do
describe "index" do
let(:user) { FactoryBot.create(:user) }
it "renders" do
login_user(user)
ballots_path
expect(response).to be_success
expect(response).to render_template("index")
end
end
# OR
describe "index" do
let(:user) { FactoryBot.create(:user) }
before(:each) do
login_user(user)
end
it "renders" do
ballots_path
expect(response).to be_success
expect(response).to render_template("index")
end
end
end
啊,是的,我也试过在那里运行它!我试着在测试的每一步都运行它,每次都会得到相同的错误。你能推荐一种更好的登录用户的方法吗?听起来你好像不喜欢login\u用户。@lylesconstant请分享你在It块外编写时遇到的新错误。错误应该已经更改。现在可能还有别的问题。我还添加了我喜欢的方式。不!不管我写在哪里,都是那个错误。在rspec内部描述,描述索引,或者它呈现。不过我会尝试你的其他修复,然后再报告!谢谢你给我提供了一些解决方案。