Jwan622
Jwan622

Reputation: 11649

Ruby rspec converting mock hash with strings into hash with symbol

I am in a plain ruby project and I have this mock:

  let(:mock_binance_client) { instance_double(Binance::Client::REST, time: {"serverTime": 1594138489530}) }

which says that when time is called it should return a hash with a string key. However, in my actual code this happens:

  def time
    print @client.time
    Time.at(@client.time["serverTime"] / 1000).strftime(FORMAT_DATE_WITH_MILLISECONDS)
  end
$ rspec
{:serverTime=>1594138489530}F

What is going on? How do I prevent this from happening? Or what can I do to get around this issue?

Upvotes: 2

Views: 1008

Answers (1)

Kache
Kache

Reputation: 16697

In my testing, rspec shouldn't be making that conversion -- you've definitely got something else going on.

That being said, it's easy to convert:

{ a: 1, b: 2 }.transform_keys(&:to_s)
# => { 'a' => 1, 'b' => 2 }

{ 'a' => 1, 'b' => 2 }.transform_keys(&:to_sym)
# => { a: 1, b: 2 }

You can change your code to always do the former to @client.time to coerce information into a known format.

I think you should still try to debug your specs to see why the conversion is happening though. (Is it even the object that you think it is?)

Upvotes: 2

Related Questions