Shoulda with TestUnit: Conditional Validation using Shoulda/Subject


/ Published in: Rails
Save to your folder(s)



Copy this code and paste it in your HTML
  1. class Post < ActiveRecord::Base
  2. validates_presence_of :title
  3. validates_presence_of :publishing_date, :if => :published
  4. end
  5.  
  6. class PostTest < ActiveSupport::TestCase
  7.  
  8. context "Post that is NOT going to be published" do
  9. should validate_presence_of(:title)
  10. should_not validate_presence_of(:publishing_date)
  11. end
  12.  
  13. context "Post that is going to be published" do
  14. subject do
  15. Post.new(:title => 'Valid Title', :published => true)
  16. end
  17. should validate_presence_of(:publishing_date)
  18. end
  19.  
  20. end

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.