Ruby on rails RSpec测试破坏行为
我正在尝试测试嵌套注释控制器的“销毁”操作 用户模型Ruby on rails RSpec测试破坏行为,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,我正在尝试测试嵌套注释控制器的“销毁”操作 用户模型有很多:注释,依赖::销毁 电影模型有很多:注释、从属::销毁 注释模型属于\u-to:user和:movie 这是我的评论 def create @comment = @movie.comments.new(comment_params.merge(user: current_user)) if @comment.save flash[:notice] = 'Comment successfully adde
有很多:注释,依赖::销毁
电影模型有很多:注释、从属::销毁
注释模型属于\u-to:user和:movie
这是我的评论
def create
@comment = @movie.comments.new(comment_params.merge(user: current_user))
if @comment.save
flash[:notice] = 'Comment successfully added'
redirect_to @movie
else
flash.now[:alert] = 'You can only have one comment per movie'
render 'movies/show'
end
end
def destroy
@comment = @movie.comments.find(params[:id])
if @comment.destroy
flash[:notice] = 'Comment successfully deleted'
else
flash[:alert] = 'You are not the author of this comment'
end
redirect_to @movie
end
private
def comment_params
params.require(:comment).permit(:body)
end
def set_movie
@movie = Movie.find(params[:movie_id])
end
当然,在动作之前还有:set\u movie,只在顶部有:%i[create destroy]
这是我的规范,我使用的是FactoryBot,所有工厂在其他示例中都可以正常工作,所以我认为问题出在其他地方
describe "DELETE #destroy" do
let(:user) { FactoryBot.create(:user) }
let(:movie) { FactoryBot.create(:movie) }
before do
sign_in(user)
end
it "deletes comment" do
FactoryBot.create(:comment, movie: movie, user: user)
expect do
delete :destroy, params { movie_id: movie.id }
end.to change(Comment, :count).by(-1)
expect(response).to be_successful
expect(response).to have_http_status(:redirect)
end
end
我有一个错误ActionController::UrlGenerationError:没有路由匹配{:action=>“destroy”,:controller=>“comments”,:movie_id=>1}
我认为我在specs destroy action中的地址是错误的,但如何以良好的方式定义它?您需要指定要删除的注释的id:
it "deletes comment" do
comment = FactoryBot.create(:comment, movie: movie, user: user)
expect do
delete :destroy, params { id: comment.id, movie_id: movie.id }
end.to change(Comment, :count).by(-1)
# ...
end
我想在这里用另一种方法作出贡献。有时,您必须确保删除了确切的实例(注释1,而不是注释2)
it "deletes comment" do
comment_1 = FactoryBot.create(:comment, movie: movie, user: user)
comment_2 = FactoryBot.create(:comment, movie: movie, user: user)
delete :destroy, params { id: comment_1.id, movie_id: movie.id }
expect { comment_1.reload }.to raise_error(ActiveRecord::RecordNotFound)
# ...
end