【问题标题】:How do I figure the ID of an item in my fixtures file using minitest?如何使用 minitest 在我的设备文件中计算项目的 ID?
【发布时间】: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


【解决方案1】:

你不需要那样做。

如果您只想确保获取有效线路的 URL...那么您可以获取任何线路。您不需要通过 id 获取它,例如:

 test "get show page with valid line id" do
    # Get any line - it doesn't matter which
    line = Line.first
    assert_not_nil line

    get line_url(line)
    line = assigns(:line)
    assert_equal test_line_id, line.id
    assert_response :success
  end

【讨论】:

    猜你喜欢
    • 2020-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-05
    • 2013-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多