2012-10-03 49 views
0

從Rspec的運行下面的代碼後 - 控制器,我從get方法未定義的方法'得到」爲#<:: RSpec的核心:: ExampleGroup :: Nested_1顯示Rspec的控制器

it "assigns @MyItems" do 
    my_item = mock(:mypay_items) 
    my_item = mock(MyItem) 
    MyItem.should_receive(:all).and_return(my) 
    get 'index' 
    assigns[:my_items].should eql(my_items) 
    response.should be_success 
end 

得到一個錯誤這將導致一個錯誤:

undefined method `get' for #<RSpec::Core::ExampleGroup::Nested_1:0x34b6ae0> 
+0

你可以發佈控制器規格的整個文件嗎? –

回答

5

這似乎是你沒有正確地宣佈你的規範作爲控制器的規格,這會導致HTTP請求方法(getpost等)不可用。確保你的天賦的頂部,你有這樣的:

describe PostsController do 
    ... 
end 

替換PostsController與控制器的名稱。如果還是不行,加:type => :controller

describe PostsController, :type => :controller do 
    ... 
end 

另見這個答案:undefined method `get' for #<RSpec::Core::ExampleGroup::Nested_1:0x00000106db51f8>

4

如果你使用的是「投機/功能」,你可能需要以下內容添加到您的「spec_helper .RB」

config.include RSpec::Rails::RequestExampleGroup, type: :feature 
2

我有同樣的問題,併爲我工作的解決辦法是添加需要'rspec的/ Rails的我spec_helper文件。我的所有控制器都安裝正確,添加:type =>控制器沒有幫助。

+0

我也是!不知道什麼是壞的,但這對我來說是唯一的解決方案,同時還有'type :::controller'。也許是Rails版本?使用RSpec 3.2.3和Rails 4.1.1 – caesarsol

相關問題