Testing validates_format_of with Shoulda matchers

Updated . Posted . Visible to the public.

Don't use should validate_format_of(...) because that matcher works in weird ways. Use the allow_value matcher instead:

describe Email, '#sender' do
  # > Rspec 3 should syntax
  it { should allow_value("email@addresse.foo").for(:sender) }
  it { should_not allow_value("foo").for(:sender) }
  
  # Rspec 3 expect syntax
  it { is_expected.to allow_value("email@addresse.foo").for(:sender) }
  it { is_expected.not_to allow_value("foo").for(:sender) }
end

Errors that may occur if you do use should validate_format_of(...):

NoMethodError: private method `gsub' called for /\A[a-z0-9\-_]+\z/:Regexp
Profile picture of Henning Koch
Henning Koch
Last edit
Daniel Straßner
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2010-09-02 12:42)