我有以下测试:
test "vote for candidate" do
get :vote, @candidate[:id]
assert_equals(1, @candidate.votes.count)
end
以下是我要测试的方法:
def vote
@candidate = Candidate.find(params[:id])
@candidate.votes.create
redirect_to(candidates_path)
end
我一直收到这个错误:
CandidatesControllerTest#test_vote_for_candidate:
NoMethodError: undefined method `symbolize_keys' for "358247726":String
test/controllers/candidates_controller_test.rb:35:in `block in <class:CandidatesControllerTest>'
任何朝着正确方向的推动都会有很大的帮助。
答案 0 :(得分:3)
get
方法期望一个Hash of params作为第二个参数,你传递String
。改变这一行
get :vote, @candidate[:id]
到
get :vote, id: @candidate[:id]
或者,假设@candidate
是ActiveRecord对象:
get :vote, id: @candidate.to_param