lundi 27 juillet 2015

how to test rails custom validation

I have a custom validation that checks whether a param is valid JSON or not:

 def is_valid_json
    begin
      !!JSON.parse(preferences)
    rescue
      errors.add(:preferences, "This is not valid JSON")
    end
  end

In my controller test, I want to make sure that when I send in a bad value, the status code of the response is 422. Here is the spec from my controller:

  it 'should return a 422 when validations fail' do
    put :update, {:user_preferences => { :email => @email, :preferences => 'badval' } }
    expect(response.status).to eq(422)
    res = JSON.parse(response.body)
    expect(res['error']).to_not be_blank
  end

The test fails due to an error:

Failure/Error: put :update, {:user_preferences => { :email => @email, :preferences => 'badval' } }
ActiveRecord::RecordInvalid:
Validation failed: Preferences This is not valid JSON

Controller code:

 def update
    @user_preference = UserPreference.where(email: params[:user_preferences][:email]).first
    authorize! :update, @user_preference
    @user_preference.update_attributes!(params[:user_preferences])
    render_api_response(@user_preference)
  end

When I make the request from the browser, I get a 422 return status code, so is there a reason that I can't get the same result from the test?

Aucun commentaire:

Enregistrer un commentaire