Rspec-core: RSpec::ExampleGroups 的未定义方法“控制器”

创建于 2014-05-20  ·  3评论  ·  资料来源: rspec/rspec-core

rspec-rails (3.0.0.beta2)rspec-rails (3.0.0.rc1)

规格

require 'spec_helper'
describe ApplicationController do
  controller do
    def index
      render :text => 'Ok'
    end

    def show
      raise TypeError, 'Any fake value!'
    end
  end

  it "redirects to new_sessions_path" do
    get :index
    expect(response).to redirect_to(new_session_path)
  end
end

错误

spec/controllers/application_controller_spec.rb:5:in `block (2 levels) in <top (required)>': undefined method `controller' for #<Class:0x007fed8fe69180> (NoMethodError)
    from /Users/ankitgupta/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.0.0.rc1/lib/rspec/core/example_group.rb:331:in `module_exec'
    from /Users/ankitgupta/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.0.0.rc1/lib/rspec/core/example_group.rb:331:in `subclass'
    from /Users/ankitgupta/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.0.0.rc1/lib/rspec/core/example_group.rb:227:in `block in define_example_group_method'
    from /Users/ankitgupta/Documents/projects/work/my-development/spec/controllers/application_controller_spec.rb:4:in `block in <top (required)>'
    from /Users/ankitgupta/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.0.0.rc1/lib/rspec/core/example_group.rb:331:in `module_exec'
    from /Users/ankitgupta/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.0.0.rc1/lib/rspec/core/example_group.rb:331:in `subclass'
    from /Users/ankitgupta/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.0.0.rc1/lib/rspec/core/example_group.rb:227:in `block in define_example_group_method'
    from /Users/ankitgupta/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.0.0.rc1/lib/rspec/core/dsl.rb:41:in `block in expose_example_group_alias'
    from /Users/ankitgupta/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.0.0.rc1/lib/rspec/core/dsl.rb:79:in `block (2 levels) in expose_example_group_alias_globally'
    from /Users/ankitgupta/Documents/projects/work/my-development/spec/controllers/application_controller_spec.rb:3:in `<top (required)>'
    from /Users/ankitgupta/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.0.0.rc1/lib/rspec/core/configuration.rb:1051:in `load'
    from /Users/ankitgupta/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.0.0.rc1/lib/rspec/core/configuration.rb:1051:in `block in load_spec_files'
    from /Users/ankitgupta/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.0.0.rc1/lib/rspec/core/configuration.rb:1051:in `each'
    from /Users/ankitgupta/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.0.0.rc1/lib/rspec/core/configuration.rb:1051:in `load_spec_files'
    from /Users/ankitgupta/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.0.0.rc1/lib/rspec/core/runner.rb:97:in `setup'
    from /Users/ankitgupta/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.0.0.rc1/lib/rspec/core/runner.rb:85:in `run'
    from /Users/ankitgupta/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.0.0.rc1/lib/rspec/core/runner.rb:70:in `run'
    from /Users/ankitgupta/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.0.0.rc1/lib/rspec/core/runner.rb:38:in `invoke'
    from /Users/ankitgupta/.rvm/gems/ruby-2.1.1/gems/rspec-core-3.0.0.rc1/exe/rspec:4:in `<top (required)>'
    from /Users/ankitgupta/.rvm/gems/ruby-2.1.1/bin/rspec:23:in `load'
    from /Users/ankitgupta/.rvm/gems/ruby-2.1.1/bin/rspec:23:in `<main>'
    from /Users/ankitgupta/.rvm/gems/ruby-2.1.1/bin/ruby_executable_hooks:15:in `eval'
    from /Users/ankitgupta/.rvm/gems/ruby-2.1.1/bin/ruby_executable_hooks:15:in `<main>'

最有用的评论

rspec-rails 2.x 有一些非常隐含的(对某些人来说是“神奇的”)行为,它会根据文件位置推断规范类型。 在 RSpec 3(从 RC1 开始)中,如果需要,您需要选择加入,或者自己设置规范类型:

describe ApplicationController, :type => :controller do
end

# or

RSpec.configure do |config|
  config.infer_spec_type_from_file_location!
end

有关更多信息,请参阅 rspec/rspec-rails#970。

对于执行 2.14 -> 2.99 -> 3.0 升级路径的人,有明确的弃用警告,但不幸的是,3.0 beta2 -> 3.0 RC1 不会触发。 对于那个很抱歉!

所有3条评论

rspec-rails 2.x 有一些非常隐含的(对某些人来说是“神奇的”)行为,它会根据文件位置推断规范类型。 在 RSpec 3(从 RC1 开始)中,如果需要,您需要选择加入,或者自己设置规范类型:

describe ApplicationController, :type => :controller do
end

# or

RSpec.configure do |config|
  config.infer_spec_type_from_file_location!
end

有关更多信息,请参阅 rspec/rspec-rails#970。

对于执行 2.14 -> 2.99 -> 3.0 升级路径的人,有明确的弃用警告,但不幸的是,3.0 beta2 -> 3.0 RC1 不会触发。 对于那个很抱歉!

@myronmarston 非常感谢! :+1:

@myronmarston究竟出了什么问题。 谢谢!

此页面是否有帮助?
0 / 5 - 0 等级