[rspec-users] should !=

David Chelimsky dchelimsky at gmail.com
Thu Oct 9 20:07:53 EDT 2008


On Thu, Oct 9, 2008 at 6:47 PM, Mark Wilden <mark at 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

> 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
>
>
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list