Search code examples
unit-testingruby-on-rails-5primary-keyminitestfixtures

How do I figure the ID of an item in my fixtures file using minitest?


I'm using Rails 5 with minitest. How do I know, in my test file, what the ID of one of my the items in my fixtures file is? I have the following schema

  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

Then in my test/fixtures/lines.yml, I create this 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

But the line

 assert_equal test_line_id, line.id

dies with teh error

Expected: "1"
  Actual: "980190962"

So how do I figure out the ID of teh item in my fixtures file?


Solution

  • You don't need to do it that way.

    If all you want is to make sure you are getting the URL for a valid line... then you can just get any line at all. You don't need to get it by id eg:

     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