3

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?

1
  • 1
    ah I needed to use => and not : Commented Jul 8, 2020 at 3:06

2 Answers 2

2

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?)

Sign up to request clarification or add additional context in comments.

Comments

0

Using rocket (=>) syntax fixes it for me. No idea why rspec does this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.