Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Request Specs #21

Open
venetya opened this issue Jul 5, 2017 · 1 comment
Open

Add Request Specs #21

venetya opened this issue Jul 5, 2017 · 1 comment
Assignees

Comments

@venetya
Copy link
Collaborator

venetya commented Jul 5, 2017

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 jendiamond self-assigned this Jul 18, 2017
@jendiamond jendiamond changed the title Add Tests Add Request Specs Jul 18, 2017
@jendiamond
Copy link
Member

jendiamond commented Jul 19, 2017

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.
rails/rails-controller-testing#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

  • Add the gem to the Gemfile
gem 'rspec-rails', '~> 3.4', '>= 3.4.2'
gem 'rails-controller-testing', '~> 0.0.3'
  • $ rails generate rspec:install
  • To run the tests
    $ bundle exec 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants