CodingForProduct / LoveLA

A streamlined path to discovery for L.A. locals and tourists using the Metro
https://lovela.herokuapp.com/
2 stars 0 forks source link

Add Request Specs #21

Open venetya opened 7 years ago

venetya commented 7 years ago

They have to be controller tests now??

http://blog.bigbinary.com/2016/04/19/changes-to-test-controllers-in-rails-5.html
n Rails 5, controller tests expect to receive URL instead of action. Otherwise test will throw exception URI::InvalidURIError: bad URI. This isn't working either:

test/controllers/routes_controller_test.rb:4:in `<top (required)>': superclass mismatch for class RoutesControllerTest (TypeError)

READ this:

http://rspec.info/blog/2016/07/rspec-3-5-has-been-released/

require 'test_helper'
require 'minitest/autorun'

class RoutesControllerTest < ActionDispatch::IntegrationTest

  def test_index
    get places_url
    assert_response :success
  end
end
jendiamond commented 7 years ago

The below does not work in Rails 5 because the gem doesn'r work cirrectly because of some of the changes made to Rails 5.
https://github.com/rails/rails-controller-testing/issues/5
@jasnow says: Ah ok. I found out the reason why RSpec isn't working. RSpec doesn't include ActionController::TestCase but instead includes only ActionController::TestCase::Behavior. What this gem is currently doing is to include the modules under ActionController::TestCase which is why the gem isn't working for RSpec. I'm looking into the fix right now.

Add RSpec


Examples:

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