代码之家  ›  专栏  ›  技术社区  ›  moondaisy

Rspec找不到ActiveAdmin控制器

  •  2
  • moondaisy  · 技术社区  · 7 年前

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

    应用程序/管理员/非活动用户。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
    

    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:   #
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Piers C    7 年前

    尝试要求 rails_helper spec_helper 看见 How is spec/rails_helper.rb different from spec/spec_helper.rb? Do I need it?

    rails\u助手 应要求 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) 这将为您提供已创建的控制器列表。