Forum: RSpec should !=

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
48641c4be1fbe167929fb16c9fd94990?d=identicon&s=25 Mark Wilden (Guest)
on 2008-10-10 01:48
(Received via mailing list)
I expected 'should !=' to act the same as 'should_not =='. That turned
out
to be incorrect (by design?):

require 'spec'
require 'spec/rails'

describe "using 'should !='" do
  it "seems to treat != as the same as ==" do
    1.should != 1 # passes
    1.should != 2 # fails
  end
end
5d38ab152e1e3e219512a9859fcd93af?d=identicon&s=25 David Chelimsky (Guest)
on 2008-10-10 02:08
(Received via mailing list)
On Thu, Oct 9, 2008 at 6:47 PM, Mark Wilden <mark@mwilden.com> wrote:
> I expected 'should !=' to act the same as 'should_not =='. That turned out
> to be incorrect (by design?):

We'd love to do that, but Ruby doesn't provide us the tools we need.
As far as we know, the only way to do that would be to do string evals
on the files instead of using actual code.

Brief explanation:

Object.instance_methods.sort.grep /\=/
=> ["==", "===", "=~", "taguri="]

Note the absence of "!=".

Try this in irb:

irb(main):001:0> 5.==(5)
=> true
irb(main):002:0> 5.!=(4)
SyntaxError: compile error
(irb):2: syntax error, unexpected tNEQ
5.!=(4)
    ^

Essentially, ruby interprets this:

  5.should == 5

as this:

  5.should.==(5)

but it interprets this:

  5.should != 4

as this:

  !(5.should.==(4))

And since 5 has no way of knowing that it's part of a negated
expression, there's no way for rspec (that I know of) to handle this
as you would expect.

Sorry - we all wish it could be so. I imagine the rubinius extended
version will support it though ;)

Cheers,
David
42172acdf3c6046f84d644cb0b94642c?d=identicon&s=25 Pat Maddox (pergesu)
on 2008-10-10 02:08
(Received via mailing list)
"Mark Wilden" <mark@mwilden.com> writes:

> end
Sorry for the short message, but yeah this is known.  You can search the
mailing list archives and the tracker for other emails.  Basically, Ruby
doesn't let us do this, because it translates
should != 1
into
!(should == 1)
and since (should == 1) is a passing expectation, the thing passes.

You can get around it by using paresetree or a similar technique but
it's sloooooooow thus has not become a part of rspec.

Pat
48641c4be1fbe167929fb16c9fd94990?d=identicon&s=25 Mark Wilden (Guest)
on 2008-10-10 02:23
(Received via mailing list)
Ahh, one of the cool things about Ruby compared to the C family of
languages
comes back to bite me. :)

///ark
This topic is locked and can not be replied to.