终端命令rspec spec / controllers / teams_controller_spec.rb正在超时

时间:2013-11-27 23:59:28

标签: rspec rspec2

我正在成功运行此命令,直到我即将启动文件中的最后一个规范(描述' DELETE#destroy' do)。现在,当我运行此问题标题中列出的命令时,我的终端似乎冻结或超时。

以下是我的相关文件和终端输出。

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

工厂/ 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

1 个答案:

答案 0 :(得分:0)

我在添加所有文件后找到了答案,所以我认为最好发布一个答案以防其他人遇到这个问题。这是一个嵌套的控制器,我的意思是它与University.rb有父关联。我很难让两个id(大学和团队)得到适当的填充,而且我变得越明确,各种错误就会停止。

我意识到通过后代的实例变量访问父协会的id,我能够找到合适的资源,如下所示:

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

我能够通过不那么明确地获得早期的规范来找到合适的资源,当我在这个spec文件的末尾时,我的猜测是它开始抱怨我之前的隐式id发现

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

希望这可以帮助将来的某个人。是时候准备好吃了。