我正在学习如何使用Ruby on Rails并尝试从控制器的测试类访问灯具时遇到一些恼人的问题。不幸的是我得到了一个' TypeError:无法将符号转换为整数'尽管在Rails测试指南中遵循了正确的方法,但仍出现错误。有没有人对如何解决这个问题有任何想法。我的控制器代码示例如下所示:
class ExerciseLogsController < ApplicationController
skip_before_filter :verify_authenticity_token
def show
@exerciseLog = ExerciseLog.find(params[:id])
respond_to do | format |
format.json { render json: @exerciseLog }
format.xml { render xml: @exerciseLog }
end
end
单元测试如下所示:
require 'test_helper'
class ExerciseLogsControllerTest < ActionController::TestCase
fixtures :exercise_logs
test "get existing exercise log" do
get :show, :id => 2, :format => 'json'
assert_response :success
assert_not_nil(assigns[:exerciseLog], "Exercise log should not be nil.")
assert_equal(exercise_logs[:log2].description, assigns[:exerciseLog].description, "Log description is not the description for log 2.")
end
end
我的灯具如下所示:
log2:
id: 2
user_id: 1
description: Walking in my lunch break.
startTime: 2014-09-11 14:00:00
endTime: 2014-09-11 15:00:00
distance: 1200
答案 0 :(得分:0)
没关系。它看起来像是行:
exercise_logs[:log2].description
应该是:
exercise_logs(:log2).description