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!!
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
Before Rails 5:
post :action, id: ..., format: :json
From Rails 5 onward:
post :action, params: { id: ..., format: :json }
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With