0

Using Ecto 2.0 I'm trying to make this test pass:

defmodule PlexServer.FleetInstanceTest do
  use PlexServer.ModelCase

  alias PlexServer.FleetInstance

  @invalid_attrs %{some_random_data: "random data"}

  test "changeset with invalid attributes" do
    changeset = FleetInstance.changeset(%FleetInstance{}, @invalid_attrs)
    refute changeset.valid?
  end
end

Here's the model

defmodule PlexServer.FleetInstance do
  use PlexServer.Web, :model

  schema "fleet_instances" do
    has_many :ship_instance, PlexServer.ShipInstance

    timestamps
  end

  def changeset(model, params \\ %{}) do
    model
      |> cast(params, [])
      |> validate_required([])
  end
end

It seems cast simply ignores any data that is not in the allowed list so the changeset is considered valid, failing the test.

Alejandro Huerta
  • 1,135
  • 2
  • 17
  • 35

1 Answers1

1

Yes, Ecto.Changeset.cast/3 will ignore any fields not specified in the third argument, and as you are passing [] to validate_required, there's really no possible params that would make your current version of PlexServer.FleetInstance.changeset/2 return valid?: false.

Dogbert
  • 212,659
  • 41
  • 396
  • 397