在单元测试中获取'symbolize_keys'错误

时间:2015-03-22 18:49:03

标签: ruby-on-rails ruby unit-testing testing

我有以下测试:

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>'

任何朝着正确方向的推动都会有很大的帮助。

1 个答案:

答案 0 :(得分:3)

get方法期望一个Hash of params作为第二个参数,你传递String。改变这一行

get :vote, @candidate[:id]

get :vote, id: @candidate[:id]

或者,假设@candidate是ActiveRecord对象:

get :vote, id: @candidate.to_param