2016-11-18 7 views
1

1つのボタンをクリックして1つのディレクターに属するすべての映画情報を返す機能を追加します。実装されてうまく動作していますが、Rspecテストを書くときに何らかのエラーが発生しました。エラーメッセージは次のとおりです。見つかりませルートのRspecエラーと一致するルートがありません

(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)>' 

同様のエラーは、以下のすべてのテストのために発生したので、私は私のroutesファイルのためのいくつかの問題があるかもしれないと思います。ここで私はroutes.rbをして指定したパスは、次のとおりです。

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

そしてRSpecのファイルは次のとおりです。

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 

コントローラのメソッドは次のとおりです。

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 

は、私はちょうどRSpecのを学ぶために開始し、コメントや提案は感謝しています。

答えて

1

経路で見られるように、アクションにはパラメータ:idが必要です。代わりに:movie_idを渡しています。だから、ちょうど

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

であなたのスペック

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

にこのラインを交換して、再試行してください。

関連する問題