Forked from eliotsykes/rspec_cheatsheet_controller_spec.rb
Created
June 11, 2020 16:00
-
-
Save frizbee/b83e45f7a681ff5feabac0201161704d to your computer and use it in GitHub Desktop.
Revisions
-
eliotsykes revised this gist
Apr 12, 2017 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -77,8 +77,6 @@ expect(flash[:error]).to eq "Buying our stuff failed :-(" expect(flash[:alert]).to eq "You didn't buy any of our stuff!!!" # Query the db to assert changes persisted expect(Invoice.count).to eq(1) -
eliotsykes revised this gist
Apr 12, 2017 . 1 changed file with 7 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,13 +2,13 @@ RSpec.describe TodosController, :type => :controller do describe "GET #index" do #describe "POST #create" do #describe "GET #show" do #describe "PATCH #update" do (or PUT #update) #describe "DELETE #destroy" do #describe "GET #new" do #describe "GET #edit" do # NORMALLY, you DO NOT want render_views, or you only want to call it in -
eliotsykes revised this gist
Apr 12, 2017 . 1 changed file with 10 additions and 17 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -19,6 +19,7 @@ it "reads like a sentence (almost)" do # Available HTTP methods: post, get, patch, put, delete, head get :index params = { id: 123 } @@ -31,23 +32,15 @@ post :create, params # old non-kwarg style post :create, params: params # new kwarg style # All optional kwargs: post :create, params: {}, # hash with HTTP parameters, may be nil body: "...", # request body string, appropriately encoded (application/x-www-form-urlencoded or multipart/form-data) session: {}, # hash of parameters to store in session, may be nil. flash: {}, # hash of parameters to store in flash, may be nil. format: :json, # Request format (string or symbol), defaults to nil. as: :json # Content type must be symbol that corresponds to a mime type, defaults to nil. # Testing 404s in controllers (assuming default Rails handling of RecordNotFound) expect { delete :destroy, { id: 'unknown' } }.to raise_error(ActiveRecord::RecordNotFound) -
eliotsykes revised this gist
Apr 12, 2017 . 1 changed file with 23 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -22,11 +22,32 @@ get :index params = { id: 123 } get :edit, params # old non-kwarg style get :edit, params: params # new kwarg style params = { widget: { description: 'Hello World' } } params.merge!(format: :js) # Specify format for AJAX/JS responses (e.g. create.js.erb view) post :create, params # old non-kwarg style post :create, params: params # new kwarg style post :create, params: {}, session: {} # All kwargs accepted by post, get, patch, put, delete, head methods: # # - params: The hash with HTTP parameters that you want to pass. # This may be nil. # # - body: The request body with a string that is appropriately encoded # (application/x-www-form-urlencoded or multipart/form-data). # # - session: A hash of parameters to store in the session. This may be nil. # # - flash: A hash of parameters to store in the flash. This may be nil. # # - format: Request format. Defaults to nil. Can be string or symbol. # # - as: Content type. Defaults to nil. Must be a symbol that corresponds # to a mime type. # Testing 404s in controllers (assuming default Rails handling of RecordNotFound) expect { delete :destroy, { id: 'unknown' } }.to raise_error(ActiveRecord::RecordNotFound) -
eliotsykes revised this gist
Apr 12, 2017 . 1 changed file with 7 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,13 +2,13 @@ RSpec.describe TodosController, :type => :controller do describe "GET index" do #describe "POST create" do #describe "GET show" do #describe "PATCH update" do (or PUT update) #describe "DELETE destroy" do #describe "GET new" do #describe "GET edit" do # NORMALLY, you DO NOT want render_views, or you only want to call it in -
eliotsykes revised this gist
Dec 12, 2014 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -31,8 +31,10 @@ # Testing 404s in controllers (assuming default Rails handling of RecordNotFound) expect { delete :destroy, { id: 'unknown' } }.to raise_error(ActiveRecord::RecordNotFound) # Rails `:symbolized` status codes at end of each status code page at http://httpstatus.es/ expect(response).to have_http_status(:success) # 200 expect(response).to have_http_status(:forbidden) # 403 expect(response).to redirect_to foo_path expect(response).to render_template(:template_filename_without_extension) expect(response).to render_template(:destroy) -
eliotsykes revised this gist
Oct 11, 2014 . 1 changed file with 1 addition and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -29,10 +29,7 @@ post :create, params # Testing 404s in controllers (assuming default Rails handling of RecordNotFound) expect { delete :destroy, { id: 'unknown' } }.to raise_error(ActiveRecord::RecordNotFound) expect(response).to have_http_status(:success) # 200 expect(response).to have_http_status(:forbidden) # 403 -
eliotsykes revised this gist
Oct 8, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -55,7 +55,7 @@ # Think of assigns(:product) as @product in the controller method expect(assigns(:product)).to eq(bestseller) expect(assigns(:cat)).to be_cool # cat.cool is a boolean, google "rspec predicate matchers" expect(assigns(:employee)).to be_a_new(Employee) -
eliotsykes revised this gist
Oct 8, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -55,7 +55,7 @@ # Think of assigns(:product) as @product in the controller method expect(assigns(:product)).to eq(bestseller) expect(assigns(:cat)).to be_cool # tcat.ool is a boolean, google "rspec predicate matchers" expect(assigns(:employee)).to be_a_new(Employee) -
eliotsykes revised this gist
Oct 8, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -55,7 +55,7 @@ # Think of assigns(:product) as @product in the controller method expect(assigns(:product)).to eq(bestseller) expect(assigns(:product)).to be_cool # product.cool is a boolean, google "rspec predicate matchers" expect(assigns(:employee)).to be_a_new(Employee) -
eliotsykes revised this gist
Oct 8, 2014 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -55,8 +55,9 @@ # Think of assigns(:product) as @product in the controller method expect(assigns(:product)).to eq(bestseller) expect(assigns(:product)).to be_desirable # product.desirable is a boolean ("Google Gpredicate matchers") expect(assigns(:employee)).to be_a_new(Employee) # Asserting flash messages expect(flash[:notice]).to eq "Congratulations on buying our stuff!" -
eliotsykes revised this gist
Oct 8, 2014 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -34,7 +34,8 @@ end expect(response).to have_http_status(:success) # 200 expect(response).to have_http_status(:forbidden) # 403 expect(response).to redirect_to foo_path expect(response).to render_template(:template_filename_without_extension) expect(response).to render_template(:destroy) -
eliotsykes revised this gist
Oct 8, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -17,7 +17,7 @@ # https://www.relishapp.com/rspec/rspec-rails/v/3-1/docs/controller-specs/render-views render_views # ONLY have this if you're certain you need it it "reads like a sentence (almost)" do get :index -
eliotsykes revised this gist
Oct 8, 2014 . 1 changed file with 7 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -20,7 +20,13 @@ it "returns 200 OK success" do get :index params = { id: 123 } get :edit, params params = { widget: { description: 'Hello World' } } params.merge!(format: :js) # Specify format for AJAX/JS responses (e.g. create.js.erb view) post :create, params # Testing 404s in controllers (assuming default Rails handling of RecordNotFound) assert_raises(ActiveRecord::RecordNotFound) do -
eliotsykes revised this gist
Oct 8, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -20,7 +20,7 @@ it "returns 200 OK success" do get :index post :create, format: :js # For AJAX/JS responses (e.g. create.js.erb view) # Testing 404s in controllers (assuming default Rails handling of RecordNotFound) assert_raises(ActiveRecord::RecordNotFound) do -
eliotsykes revised this gist
Oct 8, 2014 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -61,7 +61,8 @@ # Query the db to assert changes persisted expect(Invoice.count).to eq(1) # Reload from db an object fetched in test setup when its record in db # is updated by controller method, otherwise you're testing stale data employee.reload invoice.reload product.reload -
eliotsykes revised this gist
Oct 8, 2014 . 1 changed file with 7 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -31,8 +31,14 @@ expect(response).to have_http_status(:success) expect(response).to redirect_to foo_path expect(response).to render_template(:template_filename_without_extension) expect(response).to render_template(:destroy) # Need response.body? Requires render_views call outside "it" block (see above & read given URL) expect(response.body).to match /Bestsellers/ expect(response.body).to include "Bestsellers" expect(response.headers["Content-Type"]).to eq "text/html; charset=utf-8" expect(response.headers["Content-Type"]).to eq "text/javascript; charset=utf-8" # assigns(:foobar) accesses the @foobar instance variable # the controller method made available to the view -
eliotsykes revised this gist
Oct 8, 2014 . 1 changed file with 8 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,6 +9,13 @@ #context "DELETE destroy" do #context "GET new" do #context "GET edit" do # NORMALLY, you DO NOT want render_views, or you only want to call it in # a single context. # More on render_views: # https://www.relishapp.com/rspec/rspec-rails/v/3-1/docs/controller-specs/render-views render_views # ONLY have this if you're certain you need it it "returns 200 OK success" do @@ -25,10 +32,7 @@ expect(response).to redirect_to foo_path expect(response).to render_template(:template_filename_without_extension) expect(response.body).to match /Bestsellers/ # requires render_views outside "it" block (see above) # assigns(:foobar) accesses the @foobar instance variable # the controller method made available to the view -
eliotsykes revised this gist
Oct 8, 2014 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -26,7 +26,8 @@ expect(response).to render_template(:template_filename_without_extension) render_views # render_views means views get rendered, call before request though! # More on render_views: # https://www.relishapp.com/rspec/rspec-rails/v/3-1/docs/controller-specs/render-views expect(response.body).to match /Bestsellers/ # assigns(:foobar) accesses the @foobar instance variable -
eliotsykes revised this gist
Oct 8, 2014 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -26,6 +26,7 @@ expect(response).to render_template(:template_filename_without_extension) render_views # render_views means views get rendered, call before request though! # More on render_views: https://www.relishapp.com/rspec/rspec-rails/v/3-1/docs/controller-specs/render-views expect(response.body).to match /Bestsellers/ # assigns(:foobar) accesses the @foobar instance variable -
eliotsykes revised this gist
Oct 8, 2014 . 1 changed file with 4 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -23,6 +23,10 @@ expect(response).to have_http_status(:success) expect(response).to redirect_to foo_path expect(response).to render_template(:template_filename_without_extension) render_views # render_views means views get rendered, call before request though! expect(response.body).to match /Bestsellers/ # assigns(:foobar) accesses the @foobar instance variable # the controller method made available to the view @@ -51,9 +55,6 @@ product.reload widget.reload end end -
eliotsykes revised this gist
Oct 8, 2014 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -51,6 +51,9 @@ product.reload widget.reload # render_views means views get rendered, call in setup though! render_views expect(response.body).to match /Bestsellers/ end end -
eliotsykes revised this gist
Oct 8, 2014 . 1 changed file with 14 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,11 +3,23 @@ RSpec.describe TodosController, :type => :controller do context "GET index" do #context "POST create" do #context "GET show" do #context "PATCH update" do (or PUT update) #context "DELETE destroy" do #context "GET new" do #context "GET edit" do it "returns 200 OK success" do get :index post :create, format: :js # To get the JS response (e.g. create.js.erb view) # Testing 404s in controllers (assuming default Rails handling of RecordNotFound) assert_raises(ActiveRecord::RecordNotFound) do delete :destroy, { id: 'unknown-id' } end expect(response).to have_http_status(:success) expect(response).to redirect_to foo_path @@ -38,7 +50,7 @@ invoice.reload product.reload widget.reload end end -
eliotsykes revised this gist
Oct 8, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -21,7 +21,7 @@ # Think of assigns(:product) as @product in the controller method expect(assigns(:product)).to eq(bestseller) expect(assigns(:employee)).to be_a_new(Employee) # Asserting flash messages expect(flash[:notice]).to eq "Congratulations on buying our stuff!" -
eliotsykes revised this gist
Oct 8, 2014 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -28,6 +28,8 @@ expect(flash[:error]).to eq "Buying our stuff failed :-(" expect(flash[:alert]).to eq "You didn't buy any of our stuff!!!" # Oi Eliot - TODO: add cookies and session examples here # Query the db to assert changes persisted expect(Invoice.count).to eq(1) -
eliotsykes revised this gist
Oct 8, 2014 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -24,9 +24,9 @@ expect(assigns(:employee)).to be_a_new(Employee) # Needs shoulda-matchers # Asserting flash messages expect(flash[:notice]).to eq "Congratulations on buying our stuff!" expect(flash[:error]).to eq "Buying our stuff failed :-(" expect(flash[:alert]).to eq "You didn't buy any of our stuff!!!" # Query the db to assert changes persisted expect(Invoice.count).to eq(1) -
eliotsykes revised this gist
Oct 8, 2014 . 1 changed file with 6 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -31,6 +31,12 @@ # Query the db to assert changes persisted expect(Invoice.count).to eq(1) # Reload from db an object fetched in test setup employee.reload invoice.reload product.reload widget.reload end end -
eliotsykes revised this gist
Oct 8, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -28,7 +28,7 @@ expect(flash[:error]).to eq "DBuying our stuff failed :-(" expect(flash[:alert]).to eq "DYou didn't buy any of our stuff!!!" # Query the db to assert changes persisted expect(Invoice.count).to eq(1) end -
eliotsykes revised this gist
Oct 8, 2014 . 1 changed file with 9 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -22,6 +22,15 @@ expect(assigns(:product)).to eq(bestseller) expect(assigns(:employee)).to be_a_new(Employee) # Needs shoulda-matchers # Asserting flash messages expect(flash[:notice]).to eq "DCongratulations on buying our stuff!" expect(flash[:error]).to eq "DBuying our stuff failed :-(" expect(flash[:alert]).to eq "DYou didn't buy any of our stuff!!!" # Query the db to see changes persisted expect(Invoice.count).to eq(1) end end -
eliotsykes revised this gist
Oct 8, 2014 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,6 +7,7 @@ it "returns 200 OK success" do get :index get :index, format: :js # To get the JS response (e.g. index.js.erb view) expect(response).to have_http_status(:success) expect(response).to redirect_to foo_path
NewerOlder