2

I'm trying to test a custom member action in ActiveAdmin using rspec.

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

I'm getting this error:

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

I have tried this without any luck.

Plus, if you know of any project that uses rspec to do tests on this kind of things it would be great, the doc doesn't say much.

Update: Right now it's running the active_admin initializer after the test: An error occurred while loading

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:   #
moondaisy
  • 4,303
  • 6
  • 41
  • 70

1 Answers1

4

Try requiring rails_helper instead of spec_helper, see How is spec/rails_helper.rb different from spec/spec_helper.rb? Do I need it?

rails_helper should require File.expand_path("../../config/environment", __FILE__) then config/environment should call Rails.application.initialize! and that should be loading initalizers/active_admin.rb and defining Admin::InactiveUserController before your test starts.

After initializers/active_admin has run you should be able to inspect ActiveAdmin.application.namespaces.map(&:name), which in your case should be [:admin]. If so then inspect ActiveAdmin.application.namespaces[:admin].resources.map(&:c‌​ontroller) which should give you the list of controllers that have been created.

Piers C
  • 2,880
  • 1
  • 23
  • 29
  • You're right, by requiring `rails_helper` I get `initalizers/active_admin.rb` to load before the test but I keep getting the same error – moondaisy Oct 06 '17 at 17:58
  • 1
    After initializers/active_admin has run you should be able to inspect `ActiveAdmin.application.namespaces.map(&:name)`, which in your case should be `[:admin]`. If so then inspect `ActiveAdmin.application.namespaces[:admin].resources.map(&:controller)` which should give you the list of controllers that have been created. Do you have any other tests you can compare to? – Piers C Oct 06 '17 at 18:20
  • I used this `ActiveAdmin.application.namespaces[:admin].resources.map(&:c‌​ontroller)` and realized it was `Admin::InactiveUsersController`, so I was missing an 's'. Thanks! – moondaisy Oct 06 '17 at 19:25
  • 1
    Congrats. I'm guessing you're not first or last to get stuck on this. Please consider renaming the question to "Rspec can't find ActiveAdmin controller" – Piers C Oct 06 '17 at 19:34