Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- describe Campaign do
- before :each do
- @campaign = Campaign.new
- end
- it { should validate_presence_of :position }
- end
- class Campaign < ActiveRecord::Base
- attr_accessible :position
- validates :position, presence: true
- default_scope order(:position)
- before_validation :next_position
- # sets the position to the next id (1 if none exist) before validation
- def next_position
- if self.position.blank?
- self.position = Campaign.select("coalesce(max(position),0) + 1 as position").reorder(nil).first.position
- end
- end
- end
- Failures:
- 1) Campaign should require position to be set
- Failure/Error: it { should validate_presence_of :position }
- Expected errors to include "can't be blank" when position is set to nil, got errors: ["name can't be blank (nil)"]
- # ./spec/models/campaign_spec.rb:9:in `block (2 levels) in <top (required)>'
- it 'populates position if blank before validations' do
- subject.position = nil
- subject.valid?
- subject.position.should_not be_nil
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement