tag给出错误:crypto.rand_bytes / 1未定义或私有

时间:2017-09-22 18:20:03

标签: elixir phoenix-framework

我在我的测试中添加了login_as标签,凤凰开始提供异常:

defmodule Rumbl.VideoControllerTest do
  use Rumbl.ConnCase

  setup %{conn: conn} = config do
    if username = config[:login_as] do
      user = insert_user(username: username)
      conn = assign(conn(), :current_user, user)
      {:ok, conn: conn, user: user}
    else
      :ok
    end
  end

  @tag login_as: "max"
  test "list all user's videos on index", %{conn: conn, user: user} do
    user_video = insert_video(user, title: "funny cats")
    other_video = insert_video(insert_user(username: "other"), title: "another video")

    conn = get conn, video_path(conn, :index)
    assert html_response(conn, 200) =~ ~r/Listing Videos/
    assert String.contains?(conn.resp_body, user_video.title)
    refute String.contains?(conn.resp_body, other_video.title)
  end

  test "requires user authentication on all actions", %{conn: conn} do
    Enum.each([
      get(conn, video_path(conn, :new)),
      get(conn, video_path(conn, :index)),
      get(conn, video_path(conn, :show, "123")),
      get(conn, video_path(conn, :edit, "123")),
      put(conn, video_path(conn, :update, "123", %{})),
      post(conn, video_path(conn, :create, %{})),
      delete(conn, video_path(conn, :delete, "123")),
    ], fn conn ->
      assert html_response(conn, 302)
      assert conn.halted
    end)
  end
end

它在第15行抱怨,这是@tag login_as:“max”行,它在标签之前工作

mmm我不确定这个错误对rand_seed意味着什么,因为我没有使用任何加密函数:

  

1)测试列表索引上所有用户的视频(Rumbl.VideoControllerTest)        测试/控制器/ video_controller_test.exs:15        **(UndefinedFunctionError)函数:crypto.rand_bytes / 1未定义或私有。你的意思是:

       * rand_seed/0
       * rand_seed/1

 stacktrace:
   (crypto) :crypto.rand_bytes(8)
   (rumbl) test/support/test_helpers.ex:8: Rumbl.TestHelpers.insert_user/1
   test/controllers/video_controller_test.exs:6: Rumbl.VideoControllerTest.__ex_unit_setup_1/1
   test/controllers/video_controller_test.exs:1: Rumbl.VideoControllerTest.__ex_unit__/2

test_helpers.ex来源:

defmodule Rumbl.TestHelpers do

  alias Rumbl.Repo

  def insert_user(attrs \\ %{}) do
    changes = Dict.merge(%{
      name: "Some User",
      username: "user#{Base.encode16(:crypto.rand_bytes(8))}",
      password: "supersecret",
    }, attrs)

    %Rumbl.User{}
    |> Rumbl.User.registration_changeset(changes)
    |> Repo.insert!()
  end

  def insert_video(user, attrs \\ %{}) do
    user
    |> Ecto.build_assoc(:videos, attrs)
    |> Repo.insert!()
  end
end

1 个答案:

答案 0 :(得分:4)

:crypto.rand_bytes/1 was deprecated in OTP 19later removed in OTP 20。您可以改为使用:crypto.strong_rand_bytes/1

...
username: "user#{Base.encode16(:crypto.strong_rand_bytes(8))}",
...