【发布时间】:2015-06-01 06:48:06
【问题描述】:
我在测试 rating_controller 时遇到了这些错误。
1) RatingsController create action creates a rating if validations pass
Failure/Error: post :create, rating: {value: 4, user_id: user, hotel_id: hotel}
ActiveRecord::RecordNotFound:
Couldn't find Hotel without an ID
# ./app/controllers/ratings_controller.rb:6:in `create'
# ./spec/controllers/ratings_controller_spec.rb:12:in `block (4 levels) in <top (required)>'
# ./spec/controllers/ratings_controller_spec.rb:11:in `block (3 levels) in <top (required)>'
2) RatingsController create action does not create rating if validations fail
Failure/Error: post :create, rating: {value: 3}
ActiveRecord::RecordNotFound:
Couldn't find Hotel without an ID
# ./app/controllers/ratings_controller.rb:6:in `create'
# ./spec/controllers/ratings_controller_spec.rb:17:in `block (3 levels) in <top (required)>'
3) RatingsController update action updates rating if validations ok
Failure/Error: patch :update, value: 3, user_id: user.id, hotel_id: hotel.id
ActionController::UrlGenerationError:
No route matches {:action=>"update", :controller=>"ratings", :hotel_id=>"1", :user_id=>"2", :value=>"3"}
# ./spec/controllers/ratings_controller_spec.rb:25:in `block (3 levels) in <top (required)>'
我不知道他们来自哪里。如果可以的话,请帮助我。 我的 rating_controller:
class RatingsController < ApplicationController
#before_action :signed_in_user
before_filter :authenticate_user!
def create
@hotel = Hotel.find(params[:hotel_id])
@rating = Rating.new(params[:rating])
@rating.hotel_id = @hotel.id
@rating.user_id = current_user.id
if @rating.save
redirect_to hotel_path(@hotel), :notice => "Your rating has been saved"
end
end
def update
@hotel = Hotel.find(params[:hotel_id])
#@rating = current_user.ratings.find(@hotel.id)
@rating = Rating.find_by_hotel_id(@hotel.id)
if @rating.update_attributes(params[:rating])
redirect_to hotel_path(@hotel), :notice => "Your rating has been updated"
end
end
end
我的 rating_controller_spec.rb:
require "spec_helper"
describe RatingsController do
let(:rating) { FactoryGirl.create(:rating) }
let(:user) { FactoryGirl.create(:user) }
let(:hotel) { FactoryGirl.create(:hotel) }
describe "create action" do
before { sign_in rating.user }
it "creates a rating if validations pass" do
expect {
post :create, rating: {value: 4, user_id: user, hotel_id: hotel}
}.to change(Rating, :count).by(1)
end
it "does not create rating if validations fail" do
post :create, rating: {value: 3}
expect(response).to redirect_to(hotel_path(hotel))
end
end
describe "update action" do
before { sign_in hotel.user }
it "updates rating if validations ok" do
patch :update, value: 3, user_id: user.id, hotel_id: hotel.id
rating.reload
expect(rating.value).to eq(3);
end
it "updates rating if validations fail" do
end
end
end
尤其是第三个错误让我感到困惑,因为 rake routes 向我显示了评级更新操作的可用路径。
PATCH /hotels/:id(.:format) hotels#update
PUT /hotels/:id(.:format) hotels#update
谢谢!
Rails - 4.0.8
Ruby - 1.9.3p551
更新 1: 对不起路线。我的赌注。只是复制了错误的行。
rating PATCH /ratings/:id(.:format) ratings#update
PUT /ratings/:id(.:format) ratings#update
如果我启动服务器并手动测试它,我觉得没问题。
【问题讨论】:
标签: ruby-on-rails ruby activerecord rspec