Browse Source

Fixed error response codes, finished bikes#create spec

denney-fix-saving-dates
Jason Denney 10 years ago
parent
commit
9fa0017d36
  1. 6
      app/controllers/api/v1/bikes_controller.rb
  2. 22
      spec/controllers/api/bikes_controller_spec.rb

6
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

22
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

Loading…
Cancel
Save