0

code

class Foo
  include ActiveModel::Validations

  def bar
    errors.add(:base, 'WHAT THE HELL!?')
  end
end

spec

describe 'nonsense' do
  specify do
    foo = Foo.new
    foo.bar
    expect(foo).to have(1).error_on(:base)
  end
end

output

 Failure/Error: specify { expect(foo).to have(1).error_on(:base) }
   expected 1 error on :base, got 0

real proof of nonsense:

describe 'nonsense' do
  specify do
    foo = Foo.new
    foo.bar
    pus foo.errors.inspect
    expect(foo).to have(1).error_on(:base)
  end
end

===>
#<ActiveModel::Errors:0x00000109ba51a8 @base=#<Foo:0x00000109ba51f8 @errors=#<ActiveModel::Errors:0x00000109ba51a8 ...>>, @messages={:base=>["WHAT THE HELL!?"]}>              

omg...

patrick
  • 9,290
  • 13
  • 61
  • 112
  • possible duplicate of [Shoulda/RSpec: Make sure that validation message "xxx" is on :base](http://stackoverflow.com/questions/11453152/shoulda-rspec-make-sure-that-validation-message-xxx-is-on-base) – BBonifield Dec 04 '14 at 00:39

1 Answers1

0

It appears that if you don't call the "valid?" method prior to using the "have(n).errors_on" matcher in a spec, it will always return false, even if your errors object has stuff in it!

patrick
  • 9,290
  • 13
  • 61
  • 112