ruby on rails - What to stub, in order to test an ActiveRecord validates_uniqueness_of -
i have simplified model:
class site < activerecord::base validates :primary_domain, uniqueness: true end
and have test in minitest-spec:
describe "primary_domain" "must unique" site.create(primary_domain: "example.com") s = site.new(primary_domain: "example.com") s.wont_be valid? s.errors.messages.keys.must_include :primary_domain end end
but requires few roundtrips database, find break concept of unit-test. there can stub exclude entire database-layer?
e.g. when using mocha:
describe "primary_domain" "must unique" site.stubs(:find_by).with(primary_domain: "example.com").returns([site.new]) s = site.new(primary_domain: "example.com") s.wont_be valid? s.errors.messages.keys.must_include :primary_domain end end
apparently uniqueness validator not use find_by, other check. there better candidate stub out here achieve same?
Comments
Post a Comment