Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an ActionController::UnknownFormat, rspec testing when creating new object

Recently moved to Rails 4 and getting errors that I wasn't getting before. I'm trying to test a form that I'm making using Capybara/Rspec.

When I click the button on the form, the error I get is:

Failure/Error: find(".submit.button").click
     ActionController::UnknownFormat:
       ActionController::UnknownFormat
     # ./app/controllers/office_listings_controller.rb:32:in `create'
     # ./spec/features/office_listings_spec.rb:78:in `block (3 levels) in <top (required)>'

It points to my controller which looks like:

def create
    selected_amenities = params[:amenities] || [] # empty list if no amenities checked
    @office_listing = OfficeListing.new(params[:office_listing])
    @office_listing.broker = current_broker
    p current_broker
    @office_listing.neighborhood = Neighborhood.find(params[:neighborhood_id])
    p @office_listing.neighborhood
    if @office_listing && @office_listing.save!
      @path = city_neighborhood_office_listing_path(@office_listing, :city_id => @office_listing.neighborhood.city.id, :neighborhood_id => @office_listing.neighborhood.id)
      create_amenities(@office_listing, selected_amenities)
      respond_to do |format|
        format.js
      end
    else
      @failure = "Unable to create office :-("
      respond_to do |format|
        format.js
      end
    end
  end

and it doesn't like the format.js line even though I have a create.js.erb file which is being rendered when I actually run the page.

Don't understand why the actual page is running but my test is failing.

Any thoughts would be greatly appreciated!!

like image 317
user2391426 Avatar asked Aug 01 '13 03:08

user2391426


3 Answers

I had a similar problem:

post :duplicate, id: patient.media.first.id

produced the same error:

Failure/Error: post :duplicate, id: patient.media.first.id
ActionController::UnknownFormat:
ActionController::UnknownFormat

I fixed it by adding the format to the post call:

post :duplicate, id: patient.media.first.id, format: :js
like image 141
Cosmin Dorobantu Avatar answered Sep 19 '22 12:09

Cosmin Dorobantu


Before Rails 5:

post :action, id: ..., format: :json

From Rails 5 onward:

post :action, params: { id: ..., format: :json }
like image 37
Paweł Gościcki Avatar answered Sep 21 '22 12:09

Paweł Gościcki


yep, using the format: :js solved the problem, in my case my code looks like this:

In my spec:

  params = {
    profile_id: profile.id,
    track_id: track.id,
    format: :js
  }

  post :create, params

In my controller:

  respond_to do |format|
    format.js {}
  end
like image 40
Heriberto Magaña Avatar answered Sep 18 '22 12:09

Heriberto Magaña