Why the test does not pass

Hi all

I wrote a test as follow:

test "a invalid country code" do

  changeset = Country.changeset(%Country{}, @invalid_attrs)
  assert [{:code, "should be at most 2 character(s)"}, {:en, "can't be blank"}] in errors_on(%Country{}, @invalid_attrs)

end

When I run the test, then it failed:

 1) test a invalid country code (Pubcus.CountryModelTest)
     test/models/country_model_test.exs:17
     Assertion with in failed
     code: [code: "should be at most 2 character(s)", en: "can't be blank"] in errors_on(%Country{}, @invalid_attrs)
     lhs:  [code: "should be at most 2 character(s)", en: "can't be blank"]
     rhs:  [code: "should be at most 2 character(s)", en: "can't be blank"]
     stacktrace:
       test/models/country_model_test.exs:20: (test)

What am I doing wrong? The return message seems to be the same as I defined.

Thanks

The assert X in Y expects the X to be found in collection Y. In your case X is equal to Y, while it should be an element of Y. Consider this:

   test "assert in works this way" do
     errors = [{:code, "should be at most 2 character(s)"}, {:en, "can't be blank"}]
 
     assert {:code, "should be at most 2 character(s)"} in errors
     assert {:en, "can't be blank"} in errors
     assert [{:code, "should be at most 2 character(s)"}, {:en, "can't be blank"}], errors
   end 

Those all assertions pass.

2 Likes

Thanks so much.