简体   繁体   中英

Rspec No route matches error

I want to add a feature that returning all movies info belonging to one director by clicking one button. It has been implemented and works well, but I got some error when writing Rspec tests. The error message is:

(1)MoviesController searching for movies by the same director when 'Director' info exists should post @director and @movies variables for view
 Failure/Error: get :find_movies_by_same_director, :movie_id => @movie.id, :movie_director => @movie.director

 ActionController::RoutingError:
   No route matches {:movie_id=>"1", :movie_director=>["Star Wars", "THX-1138"], :controller=>"movies", :action=>"find_movies_by_same_director"}

 # ./spec/controllers/movie_controller_spec.rb:15:in `block (4 levels) in <top (required)>'

Similar errors of no routes found occured for all the following tests, so I think there might be some problems for my routes file. Here is the path I specify in routes.rb:

   get '/movies/:id/director', to: 'movies#find_movies_by_same_director', as: 'director'

And the Rspec file is:

require "spec_helper"

describe MoviesController do
it { should respond_to(:find_movies_by_same_director) }

describe "searching for movies by the same director" do
  context "when 'Director' info exists" do
    before do
      @movie = double(:title => "Star Wars", :id => "1", :director => "George Lucas")
      Movie.stub(:find).with("1").and_return @movie
      @lucas_films = ["Star Wars", "THX-1138"]
      @movie.stub(:director).and_return @lucas_films
    end
    it "should post @director and @movies variables for view" do
      get :find_movies_by_same_director, :movie_id => @movie.id
      assigns(:director).should == @movie.director
      assigns(:movies).should == @lucas_films
    end
  end
end

The controller method is:

def find_movies_by_same_director
  @movie = Movie.find(params[:id])
  @director = @movie.director
  if (not @director.nil?) and (not @director.empty?)
    @movies = Movie.where(director: @director) if (not @director.nil?) and (not @director.empty?);
    #@movies = Movie.find_by_sql("SELECT * FROM movies i WHERE i.director == '#{@director}'")
  render :director 
  else
    flash[:notice] = "'#{@movie.title}' has no director info"
    redirect_to root_path
end

I just start to learn Rspec, so any comment or suggestion is appreciated.

As it can be seen in routes, you action expects a parameter :id . You are passing :movie_id instead. So, just replace this line in your spec

 get :find_movies_by_same_director, :movie_id => @movie.id

with

 get :find_movies_by_same_director, :id => @movie.id

and try again.

As Jagdeep Singh suggests, your action expects an :id parameter, not an :movie_id param. But the line in your spec should pass the :id inside the params.

get :find_movies_by_same_director, params: { id => @movie.id }

I was having the same problem and this answer helped me Controller spec unknown keyword: id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM