Question

I was running this command successfully until I was just about to start the last spec in the file (describe'DELETE #destroy' do). Now when I run the command listed in this question's title, it appears that my terminal freezes or times out.

Here are my associated files and terminal output.

enter image description here

Gemfile

source 'https://rubygems.org'

'ruby' '2.0.0'
gem 'rails', '3.2.14'
gem 'pg'
gem 'devise'
gem 'composite_primary_keys'
gem 'bootstrap-sass', '~> 3.0.2.0'
gem 'bootstrap-datetimepicker-rails'
gem 'twitter-bootstrap-rails', git: 'git://github.com/seyhunak/twitter-bootstrap-rails.git'

group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'
  gem 'uglifier',     '>= 1.0.3'
end

group :development, :test do
  gem 'rspec-rails', '~> 2.0'
  gem 'shoulda-matchers'
  gem 'shoulda'
  gem 'factory_girl_rails', require: false
  gem 'pry'
  gem 'pry-rails'
  gem 'pry-remote'
  gem 'guard-zeus'
  gem 'guard-rspec', require: false
end

gem 'jquery-rails'

spec_helper.rb

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'factory_girl_rails'
require 'shoulda'
require 'shoulda-matchers'


# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

RSpec.configure do |config|
  # config.include FactoryGirl::Syntax::Methods
  # FactoryGirl.find_definitions
  # ## Mock Framework
  #
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
  #
  # config.mock_with :mocha
  # config.mock_with :flexmock
  # config.mock_with :rr

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true

  # If true, the base class of anonymous controllers will be inferred
  # automatically. This will be the default behavior in future versions of
  # rspec-rails.
  config.infer_base_class_for_anonymous_controllers = false

  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  config.order = "random"
end

factories/teams.rb

FactoryGirl.define do
  factory :team do |f|
    f.name 'Bengals'
    f.sport_type 'softball'
    # f.university_id '1'
    f.association :university
  end
end

teams_controller_spec.rb

require 'spec_helper'

describe TeamsController do
  describe 'GET #index' do
    before(:each) { @university = FactoryGirl.create(:university) }

    it 'returns http success' do
      get :index, university_id: @university.id
      expect(response).to be_success
    end

    it 'assigns every team object into an @teams array' do
      @team = FactoryGirl.create :team, university_id: @university.id
      get :index, university_id: @university.id
      expect(assigns(:teams)).to eq [@team] and be_success
    end

    it 'renders the index template' do
      @university = FactoryGirl.create :university
      get :index, university_id: @university.id
      expect(response).to render_template(:index)
    end
  end

  describe 'GET #new' do
    before(:each) { @university = FactoryGirl.create(:university) }
    it 'returns http success and instantiates a @team' do
      @university = FactoryGirl.create :university
      @team = FactoryGirl.create :team
      get :new, university_id: @university.id, id: @team.id
      expect(@team).to be_a_kind_of Team
    end

    it 'renders the #new template' do
      @university = FactoryGirl.create :university
      get :new, university_id: @university.id
      expect(response).to render_template(:new)
    end
  end

  describe 'GET #create' do
    context 'given valid credentials' do
      before(:each) { @university = FactoryGirl.create(:university) }

      it 'returns http success and redirects to the #show template' do
        @university = FactoryGirl.create :university
        post :create, team: FactoryGirl.attributes_for(:team), 
        university_id: @university.id
        team = Team.order(:created_at).last
        expect(response).to be_redirect
      end
    end

    context 'given invalid credentials' do
      before(:each) { @university = FactoryGirl.create(:university) }

      it 'returns http client error' do
        post :create, team: FactoryGirl.attributes_for(:team), 
        university_id: @university.id
        team = Team.order(:created_at).last
        expect(response).not_to be_success
      end

      it 'should render the #new template' do
        post :create, university_id: @university.id
        expect(response).to render_template(:new)
      end
    end
  end

  describe 'GET #show' do
    before(:each) { @university = FactoryGirl.create(:university) }

    it 'returns http success and renders the #show template' do
      @team = FactoryGirl.create :team
      get :show, university_id: @team.university_id, id: @team.id
      expect(response).to be_success
      expect(response).to render_template(:show)
    end
  end

  describe 'GET #edit' do
    before(:each) { @university = FactoryGirl.create(:university) }

    it 'returns http success and renders the #edit template' do
      @team = FactoryGirl.create :team
      get :edit, university_id: @team.university_id, id: @team.id
      expect(response).to be_success
      expect(response).to render_template(:edit)
    end
  end

  describe 'GET #update' do
    before(:each) { @university = FactoryGirl.create(:university) }
    it 'returns http success and redirects to the #show template' do
      @team = FactoryGirl.create :team
      get :update, university_id: @team.university_id, id: @team.id
      expect(response).to eql 200
      expect(response).to be_redirect
    end
  end

  describe 'DELETE #destroy' do
    it 'should destroy object from the database and redirect to teams_path' do
      delete :destroy, university_id: @team.university_id, id: @team.id
      expect(@university.delete).to be_true
      expect(response).to redirect_to universities_path
    end
  end
end
Was it helpful?

Solution

I found the answer after adding all of the files, so I figured it best to post an answer in case someone else runs into this. This is a nested controller, and by that I mean that it has a parent association with University.rb. I was having difficulties getting both id's (University and Team) to get filled in properly, and the more explicit I became, the various errors stopped coming up.

I realized that by accessing the parent association's id through the descendant's instance variable I was able to locate the proper resource like so:

get :show, university_id: @team.university_id, id: @team.id

I was able to get the earlier specs to find the proper resource by being less explicit, and when I was at the end of this spec file, my guess is that it began to complain about my earlier implicit id finds

get :show, [@university, @team]
get :show, university_id: @university.id, id: @team.id

Hope this helps someone in the future. Time to go prepare for thanskgiving.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top