使用具有多个 belongs_to 关系长生不老药的夹具测试控制器

Testing controller using fixture with multiple belongs_to relation elixir

我目前正在尝试 运行 顺利进行测试,但我正在努力确保设置正确。

有: 属于“用户”和“项目”的“work_item”

我不知道如何创建一个可以与具有适当关系的 setup [:work_item] 一起使用的夹具。

@create_attrs %{
    comment: "some comment",
    date: ~D[2010-04-17],
    duration_in_minutes: 42,
    hourly_rate_in_cents: 42,
    project_id: , #<- THIS ONE
    user_id: #<- &this one}

  def fixture(:work_item) do
      {:ok, work_item} = Tracking.create_work_item(@create_attrs)
    work_item
  end

  def fixture_project(:project) do
    valid_project_attrs = %{name: "Some project"}
    {:ok, project} = Clients.create_project(valid_project_attrs)
    project
  end

# Removed code for readability

defp create_work_item(_) do
    work_item = fixture(:work_item)
    %{work_item: work_item}
  end

  defp create_project(_) do
    project = fixture_project(:project)
    %{project: project}
  end

  defp create_user(_) do
    user = fixture_user(:user)
    %{user: user}
  end

如果有人能详细说明就好了!

我所做的是创建相关的灯具,然后将 @create_attrs 与指向相关灯具的指针的属性合并

def fixture(:work_item) do
  project = fixture_project(:project)
  user = fixture_user(:user)
  
  {:ok, work_item} = @create_attrs
  |> Map.merge(%{project_id: project.id, user_id: user.id})
  |> Tracking.create_work_item()

  work_item
end