2010-06-25 67 views
5

不知道這是Rails 3問題還是RSpec 2問題,但我似乎無法得到標準控制器測試工作 - 似乎是'get'方法找不到。不能得到控制器測試使用RSpec 2和邊緣Rails 3

我有一個控制器的測試看起來像這樣(在spec/controllers目錄中名爲discrepancies_controller_spec.rb):

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修改數據的安全影響的 - 這是一個獨立的問題...)

當我用耙子運行它,我得到以下錯誤:

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 patchlevel 249),[i386-wingw32],edge Rails 3和MongoDB/MongoMapper。這裏是我安裝的寶石的名單(通過 '捆綁寶石' - 我的所有已安裝的寶石被安裝捆紮機)

  • 抽象(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-紅寶石(2.1.2)
  • BSON(1.0.3)
  • bson_ext(1.0.1)
  • 助洗劑(2.1.2)
  • 打捆(0.9.26)
  • Capistrano的(2.5.19)
  • 水豚(0.3.8 a94f99)
  • 黃瓜(0.8.3)
  • 黃瓜軌(0.3.2主-b 75110)
  • culerity(0.2.10)
  • database_cleaner(0.5.2 7ea99d)
  • 色器件(1.1.rc1 88ab2f)
  • DIFF-LCS(1.1.2)
  • erubis(2.6.5 )
  • factory_girl(1.3.0)
  • factory_girl_rails(1.0)
  • 騙子(0.3.1)
  • FFI(0.6.3)
  • 小黃瓜(2.0.2)
  • highline(1.5。2)
  • I18N(0.4.1)
  • jnunemaker-可驗證(1.8.4)
  • 接頭(0.3.2 11a094)
  • json_pure(1.4.3)
  • 郵件(2.2.5)
  • MIME類型(1.16)
  • 蒙戈(1.0.3)
  • mongo_mapper(0.8.2)
  • 淨SCP(1.0.2)
  • 淨SFTP(2.0.4)
  • 淨SSH(2.0.23)
  • 淨SSH-網關(1.0.1)
  • 引入nokogiri(1.4.2.1)
  • 勇敢(0.3.2 )
  • 通曉(0.3.1)
  • 機架(1.1.0)
  • 機架式(0.6.6)
  • 架測試(0.5.4)
  • 導軌(3.0.0。 beta4 6682cc)
  • railties(3.0.0.beta4)
  • 耙(0.8.7)
  • rspec的(2.0.0.beta.13)
  • rspec的芯(2.0.0.beta.13)
  • rspec的-預期(2.0.0.beta.13)
  • rspec的-嘲笑(2.0.0.beta.13)
  • rspec的護欄(2.0.0.beta.13)
  • 硒的webdriver(0.0 .24)
  • term-ansicolor(1.0.5)
  • 托爾(0.13.6)
  • 樹梢(1.4.8)
  • trollop(1.16.2)
  • tzinfo(0.3.22)
  • 杆(0.2.1)
  • 區長(0.10。 7)
  • webrat(0.7.1)

我還加了把語句測試顯示什麼方法測試夾具((methods - Object.methods).sort.inspect)可供選擇,並且「得到」是不是在列表中。下面是在列表中:

"__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?" 

回答

4

得到了大衛赫利姆斯基RSpec的郵件列表上的答案,如下:

我很驚訝,這是第一次遇到這種已經拿出了rspec-2,但我們在這裏:)

這是我將在下一個版本中解決的路徑分隔符錯誤。目前,您可以在您的控制器規格中執行此操作:

describe DiscrepanciesController do 
    include RSpec::Rails::ControllerExampleGroup 

這應該可以正常工作。

如果你想要做一個更具全球性的解決辦法,將它添加到您的spec_helper配置:

RSpec.configure do |c| 
    c.include RSpec::Rails::ControllerExampleGroup, :example_group => { :file_path => /\bspec[\\\/]controllers[\\\/]/ } 
end