Skip to content

Instantly share code, notes, and snippets.

@jendiamond
Last active May 12, 2019 23:52
Show Gist options
  • Save jendiamond/c948aefb138c6f63a7b58bcdc203b1b3 to your computer and use it in GitHub Desktop.
Save jendiamond/c948aefb138c6f63a7b58bcdc203b1b3 to your computer and use it in GitHub Desktop.

Request Specs

Request specs provide a thin wrapper around Rails' integration tests, and are designed to drive behavior through the full stack, including routing (provided by Rails) and without stubbing (that's up to you). s assertions:


ugly_food_finder/spec/factories/markets.rb

FactoryGirl.define do
  factory :market do
    store "myStore"
    address "123 address"
    description "nice"
    delivery true
  end
end

ugly_food_finder/spec/requests/markets_spec.rb

require 'rails_helper'

describe "markets", type: :request do
  let!(:market) { FactoryGirl.create(:market) }
  let(:user) { FactoryGirl.create(:user) }
  let(:user_params) { { email: user.email, password: user.password } }

  context "when logged in" do
    before do
      post '/log_in', user_params
    end

    describe 'reading markets' do
      it "should render markets index template" do
        get '/markets'
        expect(response).to have_http_status(200)
        expect(response).to render_template('index')
      end
    end

    describe 'GET /markets/new' do
      it "should render markets new template" do
        get '/markets/new'
        expect(response).to have_http_status(200)
        expect(response).to render_template('new')
      end
    end

    describe 'POST /markets' do
      it "should create a new market" do
        expect {
          post '/markets', market: { store: market.store,
                                    address: market.address,
                                    description: market.description,
                                    delivery: market.delivery }
        }.to change(Market, :count).by(1)
        expect(response).to have_http_status(302)
        expect(response).to redirect_to(market_url(Market.last.id))
      end
    end

    describe 'GET /markets/:id' do
      before do
        post '/markets', market: { store: market.store,
                                  address: market.address,
                                  description: market.description,
                                  delivery: market.delivery }
      end

      it "should render market show template" do
        get "/markets/#{Market.last.id}"
        expect(response).to have_http_status(200)
        expect(response).to render_template('show')
      end
    end

    describe 'GET /markets/:id/edit' do
      it "should render markets edit template" do
        get "/markets/#{market.id}/edit"
        expect(response).to have_http_status(200)
        expect(response).to render_template('edit')
      end
    end

    describe 'POST /markets/:id' do
      before do
        post '/markets', market: { store: market.store,
                                  address: market.address,
                                  description: market.description,
                                  delivery: market.delivery }
      end

      it "should update a market" do
        expect {
          patch "/markets/#{market.id}", market: { store: "update store",
                                    address: "San Francisco",
                                    description: "New Description",
                                    delivery: false }
        }.to change(Market, :count).by(0)
        expect(response).to have_http_status(302)
        expect(response).to redirect_to(market_url(market))
      end
    end

    describe 'DELETE' do
      before do
        post '/markets', market: { store: market.store,
                                  address: market.address,
                                  description: market.description,
                                  delivery: market.delivery }
      end

      it "should delete a market" do
        expect {
          delete "/markets/#{Market.last.id}"
        }.to change(Market, :count).by(-1)
        expect(response).to have_http_status(302)
      end
    end
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment