【问题标题】:Testing rails controller with rspec within a plugin在插件中使用 rspec 测试 rails 控制器
【发布时间】:2010-07-22 12:27:49
【问题描述】:

我正在编写一个 Rails 插件,我希望能够在插件中测试一个控制器:

describe ReportsController, :type => :controller do

  it "shows paginated reports if the user is authorized" do
    get 'index'
    response.should render_template("index")
  end

end

不幸的是,这会导致以下错误:

NoMethodError in 'ReportsController index action shows paginated reports if the user is authorized'
undefined method `get' for #<Spec::Example::ExampleGroup::Subclass_1::Subclass_1:0x7f7155e042a0>

插件的 spec_helper.rb 中的 Rails env 已加载:

ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'
require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb'))

有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails rspec ruby-on-rails-plugins


    【解决方案1】:

    通过添加解决:

    require File.expand_path(File.join(ENV['RAILS_ROOT'], 'spec/spec_helper.rb'))
    

    到 plugin_dir/spec/spec_helper.rb

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多