【发布时间】:2018-01-07 19:25:49
【问题描述】:
我正在使用带有 minitest 的 Rails 5。在我的测试文件中,我如何知道我的设备文件中的一项的 ID 是什么?我有以下架构
create_table "lines", force: :cascade do |t|
t.string "route_long_name"
t.string "name"
t.integer "system_type"
t.string "color"
t.string "onestop_id"
t.string "vehicle_type"
t.string "wheelchair_accessible"
t.string "bikes_allowed"
t.index ["onestop_id"], name: "index_lines_on_onestop_id"
t.index ["route_long_name"], name: "index_lines_on_route_long_name"
end
然后在我的 test/fixtures/lines.yml 中,我创建了这个fixture
one:
id: "1"
name: "line1"
color: "green"
So in my minitest test, I try this
test "get show page with valid line id" do
test_line_id = "1"
line = Line.find_by_id(test_line_id)
puts "inspect1: #{line.inspect}"
assert_not_nil line
get line_url(line)
line = assigns(:line)
puts "inspect2 #{line.inspect}"
assert_equal test_line_id, line.id
assert_response :success
end
但是行
assert_equal test_line_id, line.id
死于错误
Expected: "1"
Actual: "980190962"
那么我如何在我的设备文件中找出项目的 ID?
【问题讨论】:
-
你为什么使用“1”。?改用 1 因为 rails 默认将 id 存储为整数值。在第 3 行之后使用 byebug 来检查您是否能够通过 id 获取行? "line = Line.find_by_id(test_line_id)"
标签: unit-testing ruby-on-rails-5 primary-key minitest fixtures