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

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

Posted by: admin November 30, 2017 Leave a comment

Questions:

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?

Answers:

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.

Questions:
Answers:

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

expect(target).to be < 6

Questions:
Answers:

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

Leave a Reply

Your email address will not be published. Required fields are marked *