在单元测试中出现 'symbolize_keys' 错误

Getting 'symbolize_keys' error in unit test

我有以下测试:

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

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

get 方法需要参数哈希作为第二个参数,您传递的是 String。更改此行

get :vote, @candidate[:id]

进入

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

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

get :vote, id: @candidate.to_param