diff --git a/app/controllers/api/v1/bikes_controller.rb b/app/controllers/api/v1/bikes_controller.rb index 54e7f12..b2ada0f 100644 --- a/app/controllers/api/v1/bikes_controller.rb +++ b/app/controllers/api/v1/bikes_controller.rb @@ -5,19 +5,17 @@ class Api::V1::BikesController < Api::V1::BaseController before_filter :check_bike_permission def create - puts params.inspect - if bike = params[:bike] b = Bike.new(bike) if !b.save - render json: { errors: b.errors }, status: 409 and return + render json: { errors: b.errors }, status: 422 and return else render json: { bike: b.as_json }, status: 200 and return end else - render json: { errors: ["Expected bike in submitted data" ]}, status: 400 and return + render json: { errors: ["Expected bike in submitted data" ]}, status: 422 and return end render json: {}, status: 200 and return end diff --git a/spec/controllers/api/bikes_controller_spec.rb b/spec/controllers/api/bikes_controller_spec.rb index 30592e9..3f99c86 100644 --- a/spec/controllers/api/bikes_controller_spec.rb +++ b/spec/controllers/api/bikes_controller_spec.rb @@ -29,7 +29,7 @@ describe Api::V1::BikesController do context "with no bike in json data" do it "returns 400" do post :create - expect(@response.code.to_i).to eql 400 + expect(@response.code.to_i).to eql 422 end it "returns an error message" do @@ -66,6 +66,26 @@ describe Api::V1::BikesController do expect(json.to_s).to include(@submit_json[:bike][:serial_number]) end end + + context "with invalid bike in json data" do + before(:each) do + @submit_json = { bike: { + serial_number: "XKCD", + }} + end + + it "returns 422" do + post :create, @submit_json + expect(@response.code.to_i).to eql 422 + end + + it "returns the fields with errors" do + post :create, @submit_json + json = JSON.parse(@response.body) + expect(json).to have_key("errors") + expect(json.to_s).to include("can't be blank") + end + end end end end