mirror of
				https://github.com/fspc/BikeShed-1.git
				synced 2025-10-31 00:45:35 -04:00 
			
		
		
		
	Fixed error response codes, finished bikes#create spec
This commit is contained in:
		
							parent
							
								
									e0041662d3
								
							
						
					
					
						commit
						9fa0017d36
					
				| @ -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 | ||||
|  | ||||
| @ -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…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user