Skip to content

Instantly share code, notes, and snippets.

@awd
Created July 24, 2012 17:34
Show Gist options
  • Save awd/3171375 to your computer and use it in GitHub Desktop.
Save awd/3171375 to your computer and use it in GitHub Desktop.
wrap validation rules in a context
require 'test_helper'
class LikeTest < ActiveSupport::TestCase
should belong_to(:viewable)
should belong_to(:user)
context "validations" do
subject { Like.create(user_id: 1, viewable_id: 2, liked: false) }
should validate_uniqueness_of(:user_id).scoped_to(:viewable_id)
end
end
@kalupa
Copy link

kalupa commented Jul 24, 2012

ProTip: When testing a model's validation rules, you can wrap them inside a context to setup the expected objects / subject

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment