Greg
Greg

Reputation: 328

Rspec testing ActiveAdmin create action

My rails application has model Institution that only an AdminUser can create. My app/admin/institution.rb file looks like this:

ActiveAdmin.register Institution do

  permit_params :name, :email

  index do
    id_column
    %W(name email).each do |field|
      column field.to_sym
    end
    actions
  end

  form do |f|
    f.inputs do
      f.input :name
      f.input :email
      f.actions
    end

  end

end

and the corresponding route for creating an institution is POST /admin/institutions.

Is there a simple way to test this route/action in rspec? There are very few resources online about testing ActiveAdmin. All I could find was this, this and this, the last of which seemed closest to what I am looking for, but not exactly.

I just want a way to test the POST /admin/institutions endpoint, passing it only the two required params (name and email). However the following rspec test fails:

describe Admin::InstitutionsController, :type => :controller do

  before(:each) do
    @user = FactoryGirl.create(:admin_user)
    sign_in @user
  end

  it 'creates an institution' do
    post :create, name: "test_name", email: "[email protected]"
    expect(Institution.all.count).to eq(1)
  end

end

No institution is being created but I do not get any errors. Any help is appreciated, thanks.

-Greg

Upvotes: 1

Views: 2770

Answers (1)

Charles Maresh
Charles Maresh

Reputation: 3373

Try namespacing the parameters with the institution key. In most resource based forms in Rails the form fields are grouped by the model's name. Give the following a try:

it 'creates an institution' do
  post :create, institution: { name: "test_name", email: "[email protected]" }
  expect(Institution.all.count).to eq(1)
end

The reason is how the form renders those two fields as text inputs (check the HTML source). The field's names should be:

  • institution[name]
  • institution[email]

Upvotes: 2

Related Questions