Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionController::UnknownFormat with rspec and post request

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?

like image 401
PascalTurbo Avatar asked Feb 24 '14 07:02

PascalTurbo


1 Answers

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

like image 94
palinium Avatar answered Sep 22 '22 10:09

palinium