6
我有一個奇怪的情況。 我有我的routes.rb這樣的代碼:Rpsec「無路線匹配」
concern :votable do
post :vote_up, on: :member, controller: :votes
post :vote_down, on: :member, controller: :votes
end
resources :questions, concerns: [:commentable, :votable], shallow: true do
resources :answers, concerns: [:commentable, :votable]
end
所以,它給我的助手通過 '後' vote_up_question
和vote_up_answer
:
vote_up_answer POST /answers/:id/vote_up(.:format) votes#vote_up
vote_down_answer POST /answers/:id/vote_down(.:format) votes#vote_down
vote_up_question POST /questions/:id/vote_up(.:format) votes#vote_up
vote_down_question POST /questions/:id/vote_down(.:format) votes#vote_down
我votes_controller:
before_action :load_parent
def vote_up
current_user.vote_for(@parent)
redirect_to :back, notice: "Voted up"
end
private
def load_parent
resource, id = request.path.split("/")[1, 2]
@parent = resource.singularize.classify.constantize.find(id)
end
一切完美的作品,但! 我想測試load_parent
方法使用RSpec:
RSpec.describe VotesController, type: :controller do
let(:question) {create(:question)}
let(:answer) {create(:answer, user_id: user, question_id: question)}
let(:user) {create(:user)}
before(:each) do |example|
sign_in user if example.metadata[:sign_in]
request.env["HTTP_REFERER"] = question_path(question) if example.metadata[:redirect_back]
end
describe 'POST #vote_up', sign_in: true, redirect_back: true do
it 'should assign question to @parent' do
post vote_up_question_path(question)
expect(assigns(:parent)).to eq 'question'
end
end
end
這裏是一個問題:
ActionController::UrlGenerationError:
No route matches {:action=>"https://stackoverflow.com/questions/1/vote_up", :controller=>"votes"}
有什麼不對?我也嘗試過不同的方式做出了明確的路線,像post :vote_up, question_id: question
,但它不工作
你能告訴''耙routes''輸出? –
添加到問題的一部分。 –
你有沒有嘗試過':vote_up,id:question.id'? –