我尝试使用rspec在ActiveAdmin中测试自定义成员操作。
应用/管理/ 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
应用/规格/管理/ 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: #
答案 0 :(得分:1)
请尝试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(&:controller)
,它应该为您提供已创建的控制器列表。