3

In my phoenix controller testing, I am doing something like this,

describe "update/2" do
  setup [:create_user]
  test "Edits, and responds with the user if attributes are valid", %{conn: conn, user: user} do

    response = 
      conn
      |> put(user_path(conn, :update, user.id, @update_attrs))
      |> json_response(200)

    expected = %{
      "data" => 
        %{"currentCity" => "pune", "mobileNumber" => "1234567890"}        
      }

  assert expected == response  

end

And my response map is

%{
  "data" => %{"currentCity" => "pune",
              "mobileNumber" => "1234567890",
              "name" => "xyz",
              "gender" => "male"}}

Since my response map contains extra keys which is not present in expected map, so the assertion with == fails, so I am trying to do assertion with patter matching like this

assert expected = response

but In this case assertion is always true no matter what the are the values in expected and response.

I am confused what's happening with the pattern matching in the case of maps.

Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
chandradot99
  • 3,616
  • 6
  • 27
  • 45

3 Answers3

6

I am confused what's happening with the pattern matching in the case of maps.

When you pattern match, the pattern must be present on the left hand side. You cannot "store" a pattern. What you're doing here is matching the pattern expected with the value of response, which will always match because the expected is a variable which will match any value on the right hand side.

To fix this, you can just inline the pattern like this:

assert %{"data" => 
  %{"currentCity" => "pune", "mobileNumber" => "1234567890"}        
} = response
Dogbert
  • 212,659
  • 41
  • 396
  • 397
  • I doubt _the assertion_ would work, since on failed match there is `(MatchError) no match of right hand side value: ...` _thrown_. This will make test fail, but this is not what `assert` is for. Here it’s redundant, one might simply do `%{...} = response` to make the test fail. – Aleksei Matiushkin Nov 21 '17 at 08:47
  • Nope, `assert` has special behavior for `=`. You can try it out. :) – Dogbert Nov 21 '17 at 08:50
  • Gosh. Thank you, I never knew. It has no special meaning, btw, it is likely [just rescued](https://github.com/elixir-lang/elixir/blob/4a3bf4484d95deea6deb3ad87ba88b284c1873bf/lib/ex_unit/lib/ex_unit/case.ex#L277). – Aleksei Matiushkin Nov 21 '17 at 09:03
  • I think this is where it's implemented: https://github.com/elixir-lang/elixir/blob/4a3bf4484d95deea6deb3ad87ba88b284c1873bf/lib/ex_unit/lib/ex_unit/assertions.ex#L101. – Dogbert Nov 21 '17 at 09:14
  • I wonder why we can't use the pin operator in an assertion. Something like `assert ^expected = response`. I've tried that and I get an error saying "The following variables were pinned..." – Burak Kaymakci Jun 25 '21 at 11:26
2

One could check the values of interest explicitly:

with %{"data" => 
       %{"currentCity" => pune,
         "mobileNumber" => number}} <- response do
  assert pune == "pune"
  assert number == "1234567890"
else
  _ -> assert false
end 
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
1
  got = %{
  "data" => %{"currentCity" => "pune",
            "mobileNumber" => "1234567890",
            "name" => "xyz",
            "gender" => "male"}
  }
  assert match?(%{
   "data" =>
    %{"currentCity" => "pune", "mobileNumber" => "1234567890"}
  } ,got)

But there are drawbacks.

tokubass
  • 11
  • 1