In RSpec, if I have warnings on and have
x.should == 42
another_line_of_code
then I get a warning about
warning: useless use of == in void context
Is there anything I can do other than
bitbucket = (x.should == 42)
Use:
x.should eq(42)
Or:
x.should be == 42
Or move x.should == 42
so that it's the last line inside the it block.
For those thinking BUT WHY?
I'm a complete noob to Ruby, but here is my understanding:
The warning comes from Ruby as statements like x.should == 42
or 1 == 1
return a value. If this return value is not actually used (eg. assigned to a variable, used as an argument to a function or returned from the block) then Ruby understandably sees no point of using the statement in the first place and alerts you to this.
To test this you can launch irb with the -w flag and add the following code:
> irb -w
def example
1 == 1
:hello
end
The 1 == 1 should issue a warning about useless == in void context. Try it without the :hello line and you won't get the warning as 1 == 1
would then be used as the return statement.
I know the Op and others here already understood this. However I wanted to post this for people like myself who may be wondering why the warning was being thrown and why the solutions proposed addressed this issue.
RSpec-2 has an eq(expected)
matcher that works just like ==
without the warning:
actual.should eq(expected)
You can also use actual.should be == expected
. It simply depends on what you think looks prettier!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With