Home > Net >  What's the difference between #<foo> and #[foo] in Ruby's RSpec test doubles?
What's the difference between #<foo> and #[foo] in Ruby's RSpec test doubles?

Time:04-06

I made an open-source contribution to an older Ruby library to fix several bugs with upstream dependency changes. I would argue it works well and patches the issue, however, the RSpec tests fail with an error message I cannot make any sense of.

.....................................F...........................

Failures:

  1) MagicAdmin::Resource::Token instance methods public methods #validate
     Failure/Error: validate_public_address!(rec_address, did_token)

       #<MagicAdmin::Resource::Token:0x00007fac020ff7c0> received :validate_public_address! with unexpected arguments
         expected: (#<Double "rec_address">, "WyIweHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAw...DAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDBcIn0iXQ==")
              got: ("#[Double \"rec_address\"]", "WyIweHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAw...DAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDBcIn0iXQ==")
       Diff:
       @@ -1  1 @@
       -[#<Double "rec_address">,
        ["#[Double \"rec_address\"]",

     # ./lib/magic-admin/resource/token.rb:25:in `validate'
     # ./test/resource/token_test.rb:31:in `block (4 levels) in <top (required)>'
     # /Users/ajen/.rvm/gems/ruby-3.0.1/gems/webmock-3.14.0/lib/webmock/rspec.rb:37:in `block (2 levels) in <top (required)>'

Finished in 0.21978 seconds (files took 1.9 seconds to load)
65 examples, 1 failure

Failed examples:

rspec ./test/resource/token_test.rb:8 # MagicAdmin::Resource::Token instance methods public methods #validate

Coverage report generated for RSpec to /Users/ajen/pg/magic-admin-ruby/coverage. 633 / 643 LOC (98.44%) covered.
Stopped processing SimpleCov as a previous error not related to SimpleCov has been detected

This is a permanent, deep link to the test in question: magic-admin-ruby/0b7c37e..633516b/test/resource/token_test.rb#L9-L31

Specifically, the following diff puzzles me:

@@ -1  1 @@
-[#<Double "rec_address">,
 ["#[Double \"rec_address\"]",

How do I read this? What's the difference between #<foo> and #[foo] in Ruby's RSpec test doubles?

CodePudding user response:

When I look at these two lines

expected: (#<Double "rec_address">, "WyIweHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAw...DAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDBcIn0iXQ==")
     got: ("#[Double \"rec_address\"]", "WyIweHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAw...DAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDB0ZXN0MDAwMHRlc3QwMDAwdGVzdDAwMDBcIn0iXQ==")

then it looks like you expect an instance of Double, but you got a String representation of a Double. Note that the value in the second line is wrapped with double quotes and the quotes inside are escaped.

  • Related