首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法使用RSpec 2和边缘Rails 3进行控制器测试

无法使用RSpec 2和边缘Rails 3进行控制器测试
EN

Stack Overflow用户
提问于 2010-06-25 22:49:57
回答 1查看 2.6K关注 0票数 6

不确定这是Rails 3问题还是RSpec 2问题,但我似乎无法让标准控制器测试正常工作--似乎找不到' get‘方法。

我有一个如下所示的控制器测试(在spec/controllers目录中名为spec/controllers):

代码语言:javascript
运行
复制
require 'spec_helper'

describe DiscrepanciesController do
  before :each do
    Discrepancy.delete_all
  end

  it "resolves a discrepancy" do
 discrepancy = Discrepancy.create(:my_number=>"12345", :status=>"Open")

 get :resolve, :id => discrepancy.id

 retrieved_discrepancy = Discrepancy.find_by_my_number("12345")
 retrieved_discrepancy.status.should == "Resolved"
  end
end

(是的,我知道用HTTP/GET修改数据的安全性-这是另一个问题.)

当我使用rake运行它时,会得到以下错误:

代码语言:javascript
运行
复制
1) DiscrepanciesController resolves a discrepanc
    Failure/Error: Unable to find C to read failed line
    undefined method `get' for #<RSpec::Core::ExampleGroup::Nested_1:0xc9170d0 @__memoized={}>
    # ./spec/controllers/discrepancies_controller_spec.rb:38 (ignore the line number, commented out code was removed from the sample)
    # C:/Users/Patrick_Gannon/.bundle/ruby/1.8/bundler/gems/rails-16a5e918a06649ffac24fd5873b875daf66212ad-master/activesupport/lib/active_support/dependencies.rb:212:in `inject'
    # C:/Ruby187/bin/rake:19:in `load'
    # C:/Ruby187/bin/rake:19

我可以通过亲自实例化控制器并直接调用控制器操作来测试控制器操作,它可以工作,但很难看,因为我不得不模拟出respond_toparams之类的东西。

其他相关信息:我运行的是Windows 7 32位,Ruby 1.8.7 (2010-01-10修补程序249) i 386-winw32,edge Rails 3和MongoDB/MongoMapper。下面是我的已安装宝石列表(通过“bundle gem”--所有已安装的宝石都是由Bundler安装的)

(1.0.0)

  • actionmailer (3.0.0.beta4)

  • actionpack (3.0.0.beta4)

  • activemodel (3.0.0.beta4)

  • activerecord (3.0.0.beta4)

  • activeresource (3.0.0.beta4)

  • activesupport (3.0.0.beta4)

  • arel (0.4.0)

  • bcrypt-ruby抽象
  • (2.1.2) (1.0.3)
  • bson_ext (1.0.1)
  • builder (2.1.2)
  • bundler (0.9.26)
  • capistrano (2.5.19)
  • capybara (0.3.8 a94f99)
  • cucumber (0.8.3)
  • cucumber-rails (0.3.2 master-b75110)
  • culerity (0.2.10)H 242
  • devise (1.1.rc1 88ab2f)
  • diff-lcs (1.1.2)
  • erubis (2.6.5)
  • factory_girl (1.3.0)
  • factory_girl_rails (1.0)
  • faker (0.3.1)
  • ffi (0.6.3)
  • gherkin (2.0.2)

H 161

  • jnunemaker-validatable (1.8.4)
  • joint (0.3.2 11a094)
  • json_pure (1.4.3)
  • mail (2.2.5)
  • mime-types (1.16)
  • mongo (1.0.3)
  • mongo_mapper (0.8.2)
  • net-scp (1 ).0.2)
  • net-sftp (2.0.4)
  • net-ssh (2.0.23)
  • net-ssh-gateway (1.0.1)
  • nokogiri (1.4.2.1)
  • plucky (0.3.2)
  • polyglot (0.3.1)
  • 齿条(1.1.0)
  • rack-mount (0.6.6)
  • rack-test(0.5.4)
  • rails (3.0.0.beta4 6682cc)
  • railties (3.0.0.beta4)
  • rake (0.8.7)
  • rspec (2.0.0.beta.13)
  • rspec-core (2.0.0.beta.13)
  • rspec-expectations (2.0.0.beta.13)
  • rspec-mocks (2.0.0.beta.13)H 2112
  • selenium-webdriver (0.0.24)
  • term-ansicolor (1.0.5)
  • thor (0.13.6)
  • treetop (1.4.8)
  • trollop (1.16.2)
  • tzinfo (0.3.22)
  • wand (0.2.1)
  • warden (0.10.7)

H 1131webrat (0.7.1)

我还在测试中添加了一个puts语句,以显示测试夹具((methods - Object.methods).sort.inspect)中有哪些方法可用,而"get“不在列表中。下面是清单中的内容:

代码语言:javascript
运行
复制
"__memoized", "__should_for_example_group__", "__should_not_for_example_group__", "_fixture_class_names", "_fixture_path", "_fixture_table_names", "_pre_loaded_fixtures", "_setup_mocks", "_teardown_mocks", "_use_instantiated_fixtures", "_use_transactional_fixtures", "_verify_mocks", "a_kind_of", "allow_message_expectations_on_nil", "an_instance_of", "any_args", "anything", "assert", "assert_block", "assert_equal", "assert_in_delta", "assert_instance_of", "assert_kind_of", "assert_match", "assert_nil", "assert_no_match", "assert_not_equal", "assert_not_nil", 
"assert_not_same", "assert_nothing_raised", "assert_nothing_thrown", "assert_operator", "assert_raise", "assert_raises", "assert_respond_to", "assert_same", "assert_send", "assert_throws", "be", "be_a", "be_a_kind_of", "be_a_new", "be_an", "be_an_instance_of", "be_close", "be_false", "be_instance_of", "be_kind_of", "be_nil", "be_true", "boolean", "build_message", "change", "described_class", "double", "duck_type", "eq", "eql", "equal", "example", "example=", "exist", "expect", "fixture_class_names", "fixture_class_names?", "fixture_path", "fixture_path?", "fixture_table_names", 
"fixture_table_names?", "flunk", "hash_including", "hash_not_including", "have", "have_at_least", "have_at_most", "have_exactly", "include", "instance_of", "kind_of", "match", "method_missing", "method_name", "mock", "mock_discrepancy", "mock_model", "no_args", "pending", "pre_loaded_fixtures", "pre_loaded_fixtures?", "raise_error", "respond_to", "run_in_transaction?", "running_example", "satisfy", "setup_fixtures", "stub_model", "subject", "teardown_fixtures", "throw_symbol", "use_instantiated_fixtures", "use_instantiated_fixtures?", "use_transactional_fixtures", "use_transactional_fixtures?"
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-06-28 17:26:31

大卫·克里姆斯基在RSpec邮件列表中得到了这样的答复,如下所示:

我很惊讶,这是第一次推出rspec-2,但我们在这里:)

这是一个路径分隔错误,我将在下一个版本中解决。现在,您可以在控制器规范中这样做:

代码语言:javascript
运行
复制
describe DiscrepanciesController do
  include RSpec::Rails::ControllerExampleGroup

应该会很好的。

如果您想做一个更全局的解决方案,请将其添加到spec_helper配置中:

代码语言:javascript
运行
复制
RSpec.configure do |c|
  c.include RSpec::Rails::ControllerExampleGroup, :example_group => { :file_path => /\bspec[\\\/]controllers[\\\/]/ }
end
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3121927

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档