2015-11-30 46 views
1

未定義的局部變量或方法`login_user」升級到Rails4,我不能把它傳遞Rails3中

爲什麼我的控制器沒有得到擴大ControllerMacros通過這個測試色器件???

提交的問題:https://github.com/shadowbq/cartoque/commit/2fba99c6eac5f9f2f0da2dd464f475cae0bae520

<snip> 
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} 
<...snip> 

<snip> 
    # include devise helpers in controller specs 
    config.include Devise::TestHelpers, :type => :controller 
    config.extend ControllerMacros, :type => :controller 
<...snip> 

/usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/example_group.rb:675:in `method_missing': undefined local variable or method `login_user' for RSpec::ExampleGroups::BackupExclusionsController:Class (NameError) 
    from /home/shadowbq/sandbox/cartoque/spec/controllers/backup_exlusions_controller_spec.rb:5:in `block in <top (required)>' 

<snip> 
module ControllerMacros 
    def login_admin 
    before(:each) do 
     @request.env["devise.mapping"] = Devise.mappings[:admin] 
     @user = FactoryGirl.create(:admin) 
     sign_in @user 
    end 
    end 

    def login_user 
    before(:each) do 
     @request.env["devise.mapping"] = Devise.mappings[:user] 
     @user = FactoryGirl.create(:user) 
     sign_in @user 
    end 
    end 
end 
<...snip> 

這裏充滿回溯以及..

/usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/example_group.rb:675:in `method_missing': undefined local variable or method `login_user' for RSpec::ExampleGroups::BackupExclusionsController:Class (NameError) 
    from /home/shadowbq/sandbox/cartoque/spec/controllers/backup_exlusions_controller_spec.rb:5:in `block in <top (required)>' 
    from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/example_group.rb:385:in `module_exec' 
    from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/example_group.rb:385:in `subclass' 
    from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/example_group.rb:255:in `block in define_example_group_method' 
    from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/dsl.rb:43:in `block in expose_example_group_alias' 
    from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/dsl.rb:82:in `block (2 levels) in expose_example_group_alias_globally' 
    from /home/shadowbq/sandbox/cartoque/spec/controllers/backup_exlusions_controller_spec.rb:3:in `<top (required)>' 
    from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/configuration.rb:1361:in `load' 
    from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/configuration.rb:1361:in `block in load_spec_files' 
    from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/configuration.rb:1359:in `each' 
    from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/configuration.rb:1359:in `load_spec_files' 
    from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/runner.rb:102:in `setup' 
    from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/runner.rb:88:in `run' 
    from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/runner.rb:73:in `run' 
    from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/lib/rspec/core/runner.rb:41:in `invoke' 
    from /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-core-3.4.1/exe/rspec:4:in `<top (required)>' 
    from /usr/local/rvm/gems/ruby-2.2.3/bin/rspec:23:in `load' 
    from /usr/local/rvm/gems/ruby-2.2.3/bin/rspec:23:in `<main>' 
    from /usr/local/rvm/gems/ruby-2.2.3/bin/ruby_executable_hooks:15:in `eval' 
    from /usr/local/rvm/gems/ruby-2.2.3/bin/ruby_executable_hooks:15:in `<main>' 

多個文件片斷

[email protected]:/home/shadowbq/sandbox/cartoque# head -15 spec/controllers/backup_exlusions_controller_spec.rb 
require 'spec_helper' 

describe BackupExclusionsController do 

    login_user 

    before do 
    @backup_exclusion = BackupExclusion.create 
    end 

    it "gets index" do 
    get :index 
    assert_response :success 
    assert_not_nil assigns(:backup_exclusions) 
    end 

https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-3-and-4-%28and-RSpec%29

回答

3

在rspec-rails 3中,規格不再根據路徑自動設置其類型,因此您的控制器規格不再有類型:controller。您可以:

  • reneebable這與config.infer_spec_type_from_file_location!選項
  • type: :controller
明確標記您的控制器的規格
相關問題