ActionController::UnknownFormat with rspec and post request

rspec, ruby-on-rails, ruby-on-rails-3.2

Solution

You're using `json` format in controller, but you're passing `format: :js` in the test.

It should be:

`post sensors_path, sensor: @sensor_attributes, format: :json`

Problem

I'm trying to do some integration tests with rspec on rails 4 but I alway get a "ActionController::UnknownFormat" exception when running the tests. I tried two different ways: ``` Failure/Error: post sensors_path, sensor: @sensor_attributes.to_json ActionController::UnknownFormat: ActionController::UnknownFormat Failure/Error: post sensors_path, sensor: @sensor_attributes, format: :js ActionController::UnknownFormat: ActionController::UnknownFormat ``` Here is the rspec code: ``` it "should change the number of sensors" do lambda do post sensors_path, sensor: @sensor_attributes.to_json end.should change(Sensor, :count).by(1) end it "should be successful" do post sensors_path, sensor: @sensor_attributes, format: :js response.should be_success end ``` And this is the create statement of the controller: ``` def create respond_to do |format| format.json do @sensor = Sensor.new(params["sensor"]) @sensor.uuid = SecureRandom.uuid @sensor.save render nothing: true end end end ``` And the sensor_attributes: ``` before do @sensor_attributes = { name: "Testname", description: "This is a Test-Description." } end ``` And the routes: ``` resources :sensors ``` Any idea what went wrong?

Original source