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
すべてを完璧に動作しますが、しかし!
ActionController::UrlGenerationError:
No route matches {:action=>"https://stackoverflow.com/questions/1/vote_up", :controller=>"votes"}
間違っているもの:
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
そして、ここで問題です: 私はRSpecの持つload_parent
方法があることをテストしたいですか?私はまたpost :vote_up, question_id: question
のように、明確なルートを作るためにさまざまな方法を試してみましたが、それは動作しません
? –
その一部を質問に追加しました。 –
あなたは 'post:vote_up、id:question.id'を試しましたか? –