Strings as map key in tests

Hi,

I have an app routing incoming channel messages through a sort of router.

It all works fine when I run it with real world data but when I wrote a test it does not work.

The routing looks like this:

defp _do_action( "things", routing_packet) do
    
    { status, store_result } = ThingStore.selectAllById routing_packet.data_in["thing_id"]

    { :data, Streamliner.changesetStructListToMapList store_result }
  end

And this is the test:

test "list things" do
    routing_packet = %RoutingPacket{ user_id: 1, data_in: %{ "thing_id": 20 } }
    { status, data } = do_action( "things", routing_packet )
    assert :data == status
    assert is_list data
end

If I change the data_in[“thing_id”] to data_in.thing_id the test works so it seems to be that the key somehow has become an atom even though I explicitly set it as a string.

I could of cause cast it to always be a string but it feels really stupid to rewrite my code so that it can pass my test…

How can I create a map with string keys in a test?

%{"thing_id" => 20}. => is actually the separator between key and value in a map, while : is just used for the atom key syntax sugar. If there are quotes or not does not matter at all. %{"thing_id": 20} is basically %{:"thing_id" => 20}.

2 Likes

Awesome. That’s it.