gpt4 book ai didi

ruby-on-rails - Rspec 找不到 ActiveAdmin Controller

转载 作者:太空宇宙 更新时间:2023-11-03 16:15:22 25 4
gpt4 key购买 nike

我正在尝试使用 rspec 在 ActiveAdmin 中测试自定义成员操作。

app/admin/inactive_user.rb

ActiveAdmin.register InactiveUser do
member_action :activate, method: :put do
user = User.new(email: resource.email, password: generate_activation_code)
resource.destroy if user.save
send_activation_email user
redirect_to admin_inactive_users_path, notice: "#{user.email} was activated"
end
end

app/spec/admin/inactive_user_controller_spec.rb

require 'spec_helper'

describe Admin::InactiveUserController do
render_views

before do
@admin = FactoryGirl.create(:admin, confirmed_at: Date.yesterday)
authenticate_user @admin
end

describe "PUT #activate" do
before(:each) do
@inactive_user = FactoryGirl.create(:inactive_user)
put :activate, id: @inactive_user.id
end

it "should redirect to inactive users index" do
expect(response).to redirect_to(admin_inactive_users_path)
end
end
end

我收到这个错误:

NameError:
uninitialized constant Admin
# ./spec/admin/inactive_user_controller_spec.rb:3:in `<top (required)>'

我试过了this没有任何运气。

另外,如果您知道有任何项目使用 rspec 对这类事情进行测试,那就太好了,文档没有说太多。

更新:现在它在测试后运行 active_admin 初始值设定项:加载时出错

NameError:
uninitialized constant Admin
# ./spec/admin/inactive_user_controller_spec.rb:3:in `<top (required)>'

[3, 12] in /Users/lucia/Documents/frogs-api-remote/config/initializers/active_admin.rb
3: # == Site Title
4: #
5: # Set the title that is displayed on the main layout
6: # for each of the active admin pages.
7: #
=> 8: config.site_title = "Frogs App"
9:
10: # Set the link url for the title. For example, to take
11: # users to your main site. Defaults to no link.
12: #

最佳答案

尝试使用 rails_helper 而不是 spec_helper,参见 How is spec/rails_helper.rb different from spec/spec_helper.rb? Do I need it?

rails_helper 应该需要 File.expand_path("../../config/environment", __FILE__) 然后 config/environment 应该调用 Rails.application.initialize! 并且应该在测试开始之前加载 initalizers/active_admin.rb 并定义 Admin::InactiveUserController

initializers/active_admin 运行后,您应该能够检查 ActiveAdmin.application.namespaces.map(&:name),在您的情况下应该是 [:admin]。如果是,则检查 ActiveAdmin.application.namespaces[:admin].resources.map(&:c‌ ontroller),它应该会为您提供已创建的 Controller 列表。

关于ruby-on-rails - Rspec 找不到 ActiveAdmin Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46593603/

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