Is there an rspec test for exact length of an attribute?

RSpec allows this:

expect("this string").to have(5).characters

You can actually write anything instead of 'characters', it's just syntactic sugar. All that's happening is that RSpec is calling #length on the subject.

However, from your question it sounds like you actually want to test the validation, in which case I would folow @rossta's advice.

UPDATE:

Since RSpec 3, this is no longer part of rspec-expectations, but is available as a separate gem: https://github.com/rspec/rspec-collection_matchers


There is no more have matcher in the latest major release of RSpec.

As of RSpec 3.1, the correct way to test this is :

expect("Some string".length).to be(11)

Tags:

Tdd

Bdd

Rspec