From da94eaa81d0ba07d9c2d34aabf24b9ee449b6002 Mon Sep 17 00:00:00 2001 From: OsamaSayegh Date: Mon, 11 Jun 2018 07:48:58 +0300 Subject: [PATCH] REFACTOR: admin color schemes controller specs to requests (#5964) --- .../admin/color_schemes_controller_spec.rb | 52 +++++++++++-------- 1 file changed, 30 insertions(+), 22 deletions(-) rename spec/{controllers => requests}/admin/color_schemes_controller_spec.rb (57%) diff --git a/spec/controllers/admin/color_schemes_controller_spec.rb b/spec/requests/admin/color_schemes_controller_spec.rb similarity index 57% rename from spec/controllers/admin/color_schemes_controller_spec.rb rename to spec/requests/admin/color_schemes_controller_spec.rb index efc1a488108..f3c59e3c016 100644 --- a/spec/controllers/admin/color_schemes_controller_spec.rb +++ b/spec/requests/admin/color_schemes_controller_spec.rb @@ -6,7 +6,7 @@ describe Admin::ColorSchemesController do end context "while logged in as an admin" do - let!(:user) { log_in(:admin) } + let(:admin) { Fabricate(:admin) } let(:valid_params) { { color_scheme: { name: 'Such Design', colors: [ @@ -16,19 +16,24 @@ describe Admin::ColorSchemesController do } } } - describe "index" do + before do + sign_in(admin) + end + + describe "#index" do it "returns JSON" do - Fabricate(:color_scheme) - get :index, format: :json + scheme_name = Fabricate(:color_scheme).name + get "/admin/color_schemes.json" expect(response.status).to eq(200) - expect(::JSON.parse(response.body)).to be_present + schemes = JSON.parse(response.body).map { |scheme| scheme["name"] } + expect(schemes).to include(scheme_name) end end - describe "create" do + describe "#create" do it "returns JSON" do - post :create, params: valid_params, format: :json + post "/admin/color_schemes.json", params: valid_params expect(response.status).to eq(200) expect(::JSON.parse(response.body)['id']).to be_present @@ -38,45 +43,48 @@ describe Admin::ColorSchemesController do params = valid_params params[:color_scheme][:colors][0][:hex] = 'cool color please' - post :create, params: valid_params, format: :json + post "/admin/color_schemes.json", params: valid_params - expect(response).not_to be_successful + expect(response.status).to eq(422) expect(::JSON.parse(response.body)['errors']).to be_present end end - describe "update" do + describe "#update" do let(:existing) { Fabricate(:color_scheme) } it "returns success" do - ColorSchemeRevisor.expects(:revise).returns(existing) - put :update, params: valid_params.merge(id: existing.id), format: :json + put "/admin/color_schemes/#{existing.id}.json", params: valid_params expect(response.status).to eq(200) - end - it "returns JSON" do - ColorSchemeRevisor.expects(:revise).returns(existing) - put :update, params: valid_params.merge(id: existing.id), format: :json - expect(::JSON.parse(response.body)['id']).to be_present + existing.reload + new_colors = valid_params[:color_scheme][:colors] + updated_colors = existing.colors.map { |color| { name: color.name, hex: color.hex } } + + expect(new_colors & updated_colors).to eq(new_colors) + expect(existing.name).to eq(valid_params[:color_scheme][:name]) end it "returns failure with invalid params" do color_scheme = Fabricate(:color_scheme) - params = valid_params.merge(id: color_scheme.id) + params = valid_params + params[:color_scheme][:colors][0][:name] = color_scheme.colors.first.name params[:color_scheme][:colors][0][:hex] = 'cool color please' - put :update, params: params, format: :json - expect(response).not_to be_successful + + put "/admin/color_schemes/#{color_scheme.id}.json", params: params + + expect(response.status).to eq(422) expect(::JSON.parse(response.body)['errors']).to be_present end end - describe "destroy" do + describe "#destroy" do let!(:existing) { Fabricate(:color_scheme) } it "returns success" do expect { - delete :destroy, params: { id: existing.id }, format: :json + delete "/admin/color_schemes/#{existing.id}.json" }.to change { ColorScheme.count }.by(-1) expect(response.status).to eq(200) end