bananabr bananabr - 8 months ago 30
Ruby Question

RSpec be_equal not working

I am writing some rspec examples for a plain old ruby class in my rails project and I am facing the following problem.
I have this constructor:

class Server
def initialize(host='localhost',options={:port => 443, :password => '', :vpncmd_bin_path => '/usr/local/bin/vpncmd', :timeout => 5})
@host = host
@port = options[:port].present? ? options[:port] : 443
@password = options[:password].present? ? options[:password] : ''
@vpncmd_bin_path = options[:vpncmd_bin_path].present? ? options[:vpncmd_bin_path] : '/usr/local/bin/vpncmd'
@timeout = options[:timeout].present? ? options[:timeout] : 5
@hubs = {}
@hub_cache_dirty = true
@hub_password_cache = {}

This test example:

it "should have a default constructor that takes no argument" do
s =
expect( be_equal('localhost')
expect(s.port).to be_equal('443')
expect(s.timeout).to be_equal(5)
expect(s.vpncmd_bin_path).to be_equal('/usr/local/bin/vpncmd')
expect(s.password).to be_equal('')

And rspec gives me the following result with Rails 4.2.6, jruby- and 3.4.4:

1) SoftEtherSever should have a default constructor that takes no argument
Failure/Error: expect( be_equal('localhost')
expected `"localhost".equal?("localhost")` to return true, got false
# ./spec/poro/softether_spec.rb:19:in `block in (root)'

What did I do wrong?


equal? checks whether two instances are the same. But it returns false when two strings contains the same value but refers to different objects:

# => false

What you should really use is eq()

expect( eq('localhost')