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

控制器测试日期

  •  0
  • Ben  · 技术社区  · 6 年前

    具有以下基本测试(使用exïu机器):

    # factory
    def item_factory do
      %Api.Content.Item{
        title: "Some title",
        content: "Some content",
        published_at: NaiveDateTime.utc_now
      }
    end
    
    # test
    test "lists all items", %{conn: conn} do
      item = insert(:item)
      conn = get conn, item_path(conn, :index)
      assert json_response(conn, 200)["data"] == [
        %{
          "content" => item.content,
          "published_at" => item.published_at,
          "title" => item.title,
          "id" => item.id
        }
      ]
    end
    

    我在日期上出错:

    left: ... "published_at" => "2010-04-17T14:00:00.000000"
    right: ... "published_at" => ~N[2010-04-17 14:00:00.000000]
    

    "published_at" => "#{item.published_at}"

    但仍然失败:

    left: ..."published_at" => "2010-04-17T14:00:00.000000"
    right: ..."published_at" => "2010-04-17 14:00:00.000000"
    

    1 回复  |  直到 6 年前
        1
  •  3
  •   Dogbert    6 年前

    item.published_at 是一个 NaiveDateTime 结构。当它转换为JSON时,编码器(可能 Poison

    原始日期时间 String String.Chars 实施 原始日期时间 使用与ISO8601不同的表示法。

    published_at

    assert json_response(conn, 200)["data"] == [
      %{
        ...
        "published_at" => NaiveDateTime.to_iso8601(item.published_at),
        ...
      }
    ]
    
    推荐文章