Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save frizbee/b83e45f7a681ff5feabac0201161704d to your computer and use it in GitHub Desktop.

Select an option

Save frizbee/b83e45f7a681ff5feabac0201161704d to your computer and use it in GitHub Desktop.

Revisions

  1. @eliotsykes eliotsykes revised this gist Apr 12, 2017. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions rspec_cheatsheet_controller_spec.rb
    Original 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!!!"

    # Oi Eliot - TODO: add cookies and session examples here

    # Query the db to assert changes persisted
    expect(Invoice.count).to eq(1)

  2. @eliotsykes eliotsykes revised this gist Apr 12, 2017. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions rspec_cheatsheet_controller_spec.rb
    Original 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
    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
  3. @eliotsykes eliotsykes revised this gist Apr 12, 2017. 1 changed file with 10 additions and 17 deletions.
    27 changes: 10 additions & 17 deletions rspec_cheatsheet_controller_spec.rb
    Original 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
    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.

    # 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)
  4. @eliotsykes eliotsykes revised this gist Apr 12, 2017. 1 changed file with 23 additions and 2 deletions.
    25 changes: 23 additions & 2 deletions rspec_cheatsheet_controller_spec.rb
    Original file line number Diff line number Diff line change
    @@ -22,11 +22,32 @@
    get :index

    params = { id: 123 }
    get :edit, params

    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

    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)
  5. @eliotsykes eliotsykes revised this gist Apr 12, 2017. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions rspec_cheatsheet_controller_spec.rb
    Original file line number Diff line number Diff line change
    @@ -2,13 +2,13 @@

    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
    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
  6. @eliotsykes eliotsykes revised this gist Dec 12, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions rspec_cheatsheet_controller_spec.rb
    Original 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)
  7. @eliotsykes eliotsykes revised this gist Oct 11, 2014. 1 changed file with 1 addition and 4 deletions.
    5 changes: 1 addition & 4 deletions rspec_cheatsheet_controller_spec.rb
    Original 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)
    assert_raises(ActiveRecord::RecordNotFound) do
    delete :destroy, { id: 'unknown-id' }
    end

    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
  8. @eliotsykes eliotsykes revised this gist Oct 8, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rspec_cheatsheet_controller_spec.rb
    Original 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(:cat)).to be_cool # cat.cool is a boolean, google "rspec predicate matchers"
    expect(assigns(:employee)).to be_a_new(Employee)


  9. @eliotsykes eliotsykes revised this gist Oct 8, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rspec_cheatsheet_controller_spec.rb
    Original 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(:cat)).to be_cool # tcat.ool is a boolean, google "rspec predicate matchers"
    expect(assigns(:employee)).to be_a_new(Employee)


  10. @eliotsykes eliotsykes revised this gist Oct 8, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rspec_cheatsheet_controller_spec.rb
    Original 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_desirable # product.desirable is a boolean ("Google Gpredicate matchers")
    expect(assigns(:product)).to be_cool # product.cool is a boolean, google "rspec predicate matchers"
    expect(assigns(:employee)).to be_a_new(Employee)


  11. @eliotsykes eliotsykes revised this gist Oct 8, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion rspec_cheatsheet_controller_spec.rb
    Original 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!"
  12. @eliotsykes eliotsykes revised this gist Oct 8, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion rspec_cheatsheet_controller_spec.rb
    Original file line number Diff line number Diff line change
    @@ -34,7 +34,8 @@
    end


    expect(response).to have_http_status(:success)
    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)
  13. @eliotsykes eliotsykes revised this gist Oct 8, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rspec_cheatsheet_controller_spec.rb
    Original 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 "returns 200 OK success" do
    it "reads like a sentence (almost)" do

    get :index

  14. @eliotsykes eliotsykes revised this gist Oct 8, 2014. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion rspec_cheatsheet_controller_spec.rb
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,13 @@
    it "returns 200 OK success" do

    get :index
    post :create, format: :js # For AJAX/JS responses (e.g. create.js.erb view)

    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
  15. @eliotsykes eliotsykes revised this gist Oct 8, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rspec_cheatsheet_controller_spec.rb
    Original 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 # To get the JS response (e.g. create.js.erb view)
    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
  16. @eliotsykes eliotsykes revised this gist Oct 8, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion rspec_cheatsheet_controller_spec.rb
    Original 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
    # 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
  17. @eliotsykes eliotsykes revised this gist Oct 8, 2014. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion rspec_cheatsheet_controller_spec.rb
    Original 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)

    expect(response.body).to match /Bestsellers/ # requires render_views outside "it" block (see above)
    # 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
  18. @eliotsykes eliotsykes revised this gist Oct 8, 2014. 1 changed file with 8 additions and 4 deletions.
    12 changes: 8 additions & 4 deletions rspec_cheatsheet_controller_spec.rb
    Original 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)

    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/
    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
  19. @eliotsykes eliotsykes revised this gist Oct 8, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion rspec_cheatsheet_controller_spec.rb
    Original 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
    # 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
  20. @eliotsykes eliotsykes revised this gist Oct 8, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions rspec_cheatsheet_controller_spec.rb
    Original 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
  21. @eliotsykes eliotsykes revised this gist Oct 8, 2014. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions rspec_cheatsheet_controller_spec.rb
    Original 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

    # render_views means views get rendered, call in setup though!
    render_views
    expect(response.body).to match /Bestsellers/
    end

    end
  22. @eliotsykes eliotsykes revised this gist Oct 8, 2014. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions rspec_cheatsheet_controller_spec.rb
    Original 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
  23. @eliotsykes eliotsykes revised this gist Oct 8, 2014. 1 changed file with 14 additions and 2 deletions.
    16 changes: 14 additions & 2 deletions rspec_cheatsheet_controller_spec.rb
    Original 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
    get :index, format: :js # To get the JS response (e.g. index.js.erb view)
    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
  24. @eliotsykes eliotsykes revised this gist Oct 8, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rspec_cheatsheet_controller_spec.rb
    Original 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) # Needs shoulda-matchers
    expect(assigns(:employee)).to be_a_new(Employee)

    # Asserting flash messages
    expect(flash[:notice]).to eq "Congratulations on buying our stuff!"
  25. @eliotsykes eliotsykes revised this gist Oct 8, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions rspec_cheatsheet_controller_spec.rb
    Original 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)

  26. @eliotsykes eliotsykes revised this gist Oct 8, 2014. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions rspec_cheatsheet_controller_spec.rb
    Original 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 "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!!!"
    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)
  27. @eliotsykes eliotsykes revised this gist Oct 8, 2014. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions rspec_cheatsheet_controller_spec.rb
    Original 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
  28. @eliotsykes eliotsykes revised this gist Oct 8, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rspec_cheatsheet_controller_spec.rb
    Original 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 see changes persisted
    # Query the db to assert changes persisted
    expect(Invoice.count).to eq(1)

    end
  29. @eliotsykes eliotsykes revised this gist Oct 8, 2014. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions rspec_cheatsheet_controller_spec.rb
    Original 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
  30. @eliotsykes eliotsykes revised this gist Oct 8, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions rspec_cheatsheet_controller_spec.rb
    Original 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