2012-08-06 2 views
1

내 RSpec에 테스트를하고 있는데 그것은 다음과 같은 출력 것 :Rspec에서 expect 메소드가 누락 된 것 같습니다. 올바른가요?

Failure/Error: expect { 
NoMethodError: 
    undefined method `[]' for nil:NilClass 
# ./spec/controllers/users_controller_spec.rb:44:in `block (4 levels) in <top (required)>' 

오류가 참조하는 줄 것은 "{기대"라고 선이다. 나는 지구상에서 무엇이 잘못되고 있는지 잘 모르겠습니다. 잘못된 여기에 무슨 일이 일어나고 있는지에

require 'spec_helper' 

describe UsersController do 

    def valid_attributes 
     { 
     :username => "tester", 
     :email => "[email protected]", 
     :password => "testingpass" 
     } 
    end 

    def valid_session 
    {} 
    end 

    describe "POST create" do 
    describe "with valid params" do 
     it "creates a new User" do 
     expect { 
      post :create, {:user => valid_attributes , :format => :json}, valid_session 
     }.to change(User, :count).by(1) 
     end 
    end 
    end 
end 

어떤 아이디어 :

여기에 전체 스펙은?

주석 기

업데이트는 spec_helper.rb 파일을 요청했다. 아래에 게시하고 있습니다 ... rspec에 의해 생성 된 기본 설정입니다. 오류 메시지가 nil[]을 실종 말한다 -

# 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 'rspec/autorun' 

# 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| 
    # ## 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 
+0

이것은 잘 작동합니다. 'user.rb'와'spec_helper.rb'의 관련 부분을 볼 수 있습니까? – jordanpg

+0

방금 ​​프로젝트를 실행 했으므로 user.rb와 관련된 문제가 발생하지 않으며 사용자를 완벽하게 만들고 삭제합니다. 자동 생성 된 버전 인 spec_helper.rb로 업데이트를 게시하고 있습니다. –

+0

'user.rb' 또는'users_controller.rb'를 변경 했습니까? 그것은 다음으로 볼 곳입니다. – jordanpg

답변

0

그것은 expect 방법 누락 아니에요. 기본적으로 RSpec은 문제가되는 스펙의 행만 표시하지만 때로는 문제가 더 심해지는 경우도 있습니다.

명령 줄에서 --backtrace 플래그로 스펙을 실행하여 전체 백 트레이스를 확인하고 실제 소스를 찾아냅니다.

HTH, David

관련 문제