Does rspec have anything more specific than target.should be < 6?

RubyRspec

Ruby Problem Overview


http://cheat.errtheblog.com/s/rspec/ has for inequalities (such as less than or greater than)

target.should be < 6

Has anything better been created since the cheat sheet was created?

Ruby Solutions


Solution 1 - Ruby

In RSpec's new expectation syntax, you would express it as:

expect(target).to be < 6

Solution 2 - Ruby

This is still the accepted way to handle this test. It's best to use >, <, and == in my opinion for numerical comparisons -- it's clearer.

Solution 3 - Ruby

If you just want to check it in a variable like target then target.should be < 6 is the way to go.

But if you want to check a property in another object, like customer.orders, where orders is a collection of elements, then you could use the have(n).items matcher.

Example:

customer.should have_at_most(6).orders

That is the same expectation than this:

customer.orders.size.should be < 6

But with a cleaner message

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndrew GrimmView Question on Stackoverflow
Solution 1 - RubyCharles WorthingtonView Answer on Stackoverflow
Solution 2 - Rubymrb_bkView Answer on Stackoverflow
Solution 3 - RubytothemarioView Answer on Stackoverflow