Skip to content

Instantly share code, notes, and snippets.

@kalleth
Created December 4, 2013 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kalleth/7796677 to your computer and use it in GitHub Desktop.
Save kalleth/7796677 to your computer and use it in GitHub Desktop.
shoulda-matchers attempting to create errors container
require 'spec_helper'
class FakeOrder
include ActiveModel::Validations
attr_accessor :site_ids, :user_id
validate :validate_at_least_one_site_chosen
validates_presence_of :user_id
def validate_at_least_one_site_chosen
errors.add(:site, "please choose at least one site")
end
end
puts "Validating FakeOrder"
f = FakeOrder.new
f.valid?
puts "--> f.errors.messages: #{f.errors.messages}"
describe FakeOrder do
describe 'validations' do
it { should validate_presence_of :user_id }
end
end
$ be rspec spec/models/fake_order_spec.rb
Validating FakeOrder
--> f.errors.messages:
:site => please choose at least one site
FakeOrder
validations
should require user_id to be set (FAILED - 1)
Failures:
1) FakeOrder validations
Failure/Error: it { should validate_presence_of :user_id }
NoMethodError:
undefined method `site' for #<FakeOrder:0x1090b88f8>
# ./spec/models/fake_order_spec.rb:21
Finished in 0.99055 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/models/fake_order_spec.rb:21 # FakeOrder validations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment