52

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?

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338

3 Answers3

74

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

expect(target).to be < 6
Charles Worthington
  • 1,110
  • 8
  • 11
56

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.

mrb_bk
  • 726
  • 6
  • 5
8

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

tothemario
  • 5,851
  • 3
  • 44
  • 39