我创建了一个电影评论应用,允许登录用户添加和编辑电影,并为每部电影留下评论。
我正在测试我的控制器,但我一直在接受:
3) Error:
ReviewsControllerTest#test_should_get_edit:
ActionController::UrlGenerationError: No route matches
{:action=>"edit_movie_review", :controller=>"reviews"}
test/controllers/reviews_controller_test.rb:34:in `block in <class:ReviewsControllerTest>'
4) Error:
ReviewsControllerTest#test_should_get_new:
ActionController::UrlGenerationError: No route matches {:action=>"new", :controller=>"reviews"}
test/controllers/reviews_controller_test.rb:17:in `block in <class:ReviewsControllerTest>'
5) Error:
ReviewsControllerTest#test_should_show_review:
ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"reviews", :id=>"1"}
test/controllers/reviews_controller_test.rb:29:in `block in <class:ReviewsControllerTest>'
6) Error:
ReviewsControllerTest#test_should_create_review:
ActionController::UrlGenerationError: No route matches {:action=>"create", :controller=>"reviews", :review=>{:comment=>"MyText", :rating=>"1"}}
test/controllers/reviews_controller_test.rb:23:in `block (2 levels) in <class:ReviewsControllerTest>'
test/controllers/reviews_controller_test.rb:22:in `block in <class:ReviewsControllerTest>'
耙路线:
POST /movies/:movie_id/reviews(.:format) reviews#create
new_movie_review GET /movies/:movie_id/reviews/new(.:format) reviews#new
edit_movie_review GET /movies/:movie_id/reviews/:id/edit(.:format) reviews#edit
movie_review GET /movies/:movie_id/reviews/:id(.:format) reviews#show
PATCH /movies/:movie_id/reviews/:id(.:format) reviews#update
PUT /movies/:movie_id/reviews/:id(.:format) reviews#update
DELETE /movies/:movie_id/reviews/:id(.:format) reviews#destroy
movies GET /movies(.:format) movies#index
POST /movies(.:format) movies#create
new_movie GET /movies/new(.:format) movies#new
edit_movie GET /movies/:id/edit(.:format) movies#edit
movie GET /movies/:id(.:format) movies#show
PATCH /movies/:id(.:format) movies#update
PUT /movies/:id(.:format) movies#update
ReviewsControllerTest:
require 'test_helper'
class ReviewsControllerTest < ActionController::TestCase
setup do
@review = reviews(:one)
@user = users(:one)
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:review)
end
test "should get new" do
get :new
assert_response :success
end
test "should create review" do
assert_difference('Review.count') do
post :create, review: { comment: @review.comment, rating: @review.rating }
end
assert_redirected_to review_path(assigns(:review))
end
test "should show review" do
get :show, id: @review
assert_response :success
end
test "should get edit" do
get :edit_movie_review, @review
assert_response :success
end
test "should update review" do
put :update
assert_redirected_to review_path(assigns(:review))
end
test "should destroy review" do
assert_difference('Review.count', -1) do
delete :destroy, id: @review
end
assert_redirected_to reviews_path
end
end
ReviewsController.rb:
class ReviewsController < ApplicationController
before_action :find_movie
before_action :find_review, only: [:edit, :update, :destroy]
before_action :authenticate_user!, only: [:new, :edit]
def new
@review = Review.new
end
def create
@review = Review.new(review_params)
@review.user_id = current_user.id
@review.movie_id = @movie.id
if @review.save #if a review is succesfully saved, redirect user to home
redirect_to movie_path(@movie)
else
render 'new'
end
end
def edit
end
def update
if @review.update(review_params)
redirect_to movie_path(@movie)
else
render 'edit'
end
end
def destroy
@review.destroy
redirect_to movie_path(@movie)
end
private
def review_params
params.require(:review).permit(:rating, :comment)
end
def find_movie
@movie = Movie.find(params[:movie_id])
end
def find_review
@review = Review.find(params[:id])
end
end
我是编程世界的新手,因此非常感谢任何形式的建议。
提前致谢!
答案 0 :(得分:0)
您可以将movie_id
与您的路线一起传递。尝试使用此new
动作测试,您也可以按照相同的模式执行其他操作。
注意:您ReviewsController
没有index
和show
行动。
require 'test_helper'
class ReviewsControllerTest < ActionController::TestCase
setup do
@movie = movies(:one)
@review = reviews(:one)
@user = users(:one)
end
#test "should get index" do
# get :index
# assert_response :success
# assert_not_nil assigns(:review)
#end
test "should get new" do
get :new, movie_id: @movie.id
assert_response :success
end
test "should create review" do
assert_difference('Review.count') do
post :create, movie_id: @movie.id ,review: { comment: @review.comment, rating: @review.rating }
end
assert_redirected_to movie_path(assigns(:review))
end
test "should get edit" do
get :edit, movie_id: @movie.id, id: @review.id
assert_response :success
end
test "should update review" do
put :update, movie_id: @movie.id, id: @review.id, review: { comment: @review.comment, rating: @review.rating }
assert_redirected_to movie_path(assigns(:review))
end
test "should destroy review" do
assert_difference('Review.count', -1) do
delete :destroy, id: @review, movie_id: @movie.id
end
end
end