获取未定义的方法在rails上的ruby中获取命名路由

时间:2013-04-26 06:31:05

标签: ruby-on-rails rspec routes

我一直收到这个我无法弄清楚的错误:

Failure/Error: get :find_movies, {:id => @id_passed }
 NoMethodError:
   undefined method `get' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x0000000363f800>

对于以下rspec测试:

it 'should find the movie in the database by the passed id' do
        Movie.should_receive(:find).with(@id_passed).and_return(@movie_found)
        get :find_movies, {:id => @id_passed }
end

使用以下路线:

get '/movies/find/:id' => 'movies#find', :as => :find_movies

我的控制器包括:

def find
  @movie = Movie.find params[:id]
  if @movie.director != ""
    @similar = Movie.where({:director => @movie.director}).all if @movie.director
  else
    flash[:warning] = "\'#{@movie.title}\' has no director info"
    redirect_to movies_path
  end
end

我的朋友几乎写了完全相同的代码,并让它工作。任何人都可以帮我弄清楚为什么这不起作用?非常感谢!

1 个答案:

答案 0 :(得分:5)

rspec似乎并不认为您的规范是控制器规范。您可以在此类描述中添加type

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

另一种解决方法可能是将require 'rspec/rails'添加到spec_helper

如果您使用的是“规格/功能”,则可能需要在“spec_helper.rb”中添加以下内容

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

有关详情,请参阅此答案:undefined method `get' for #<RSpec::Core::ExampleGroup::Nested_1:0x00000106db51f8>