代码之家  ›  专栏  ›  技术社区  ›  Dave

如何使用minitest计算fixtures文件中项目的ID?

  •  0
  • Dave  · 技术社区  · 7 年前

    我正在使用Rails 5和minitest。在我的测试文件中,我如何知道fixtures文件中某个项目的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
    

    然后在我的测试/夹具/生产线中。yml,我创建了这个装置

    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"
    

    那么,如何在fixtures文件中计算出项目的ID呢?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Taryn East    7 年前

    你不需要那样做。

    如果您只想确保获得有效行的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