Inconsistency between `assert_equal` and `must_equal` #203

Closed
tenderlove opened this Issue Dec 6, 2012 · 3 comments

Comments

Projects
None yet
3 participants
Owner

tenderlove commented Dec 6, 2012

I am trying to test a proxy object. The proxy objects are supposed to be equal to each other when the target object is equal. assert_equal works and says that the two objects are equal, but when I use must_equal it says they are not equal.

Here is an example:

require 'minitest/autorun'

class Proxy < BasicObject
  def initialize(target)
    @target = target
  end

  def method_missing(*args, &block)
    @target.__send__(*args, &block)
  end
end

class ProxyTest < MiniTest::Unit::TestCase
  def test_equality
    xx = Proxy.new(:omg)
    assert_equal xx, xx # passes
    xx.must_equal xx    # fails
  end
end

I'm using assert_equal to work around the issue for now. Thanks!

Owner

zenspider commented Dec 6, 2012

The passing case is testing the object identity of your proxy object, which is a useless test. The failing case is resolving the proxy on the LHS but not the RHS, and they are indeed not the same thing.

What you should be doing is writing better tests (ie, not testing your proxies, but testing the objects they're proxying), or making it so you're comparing apples to apples.

One easy way around this is to have proxy object method calls resolve their arguments if they're also proxy objects:

  def method_missing(*args, &block)
    args.map! { |o| ::Proxy === o ? o.target : o } # resolve any proxy args
    @target.__send__(*args, &block)
  end

zenspider closed this Dec 6, 2012

@tenderlove this was interesting to me and I have been thinking about it.

what about overriding #==

  def ==(other)
    other == @target
  end
Owner

zenspider commented Dec 9, 2012

a bad test is a bad test... you can add lots of bandaids to try to cover it up but it doesn't change the fact.

zenspider locked and limited conversation to collaborators May 17, 2017

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.