I'm hitting a mental block. Can anyone explain to me how I can write a spec test for validate_uniqueness_of?
Or use Shoulda:
before do
@user = Factory(:user)
end
subject { @user }
it { should validate_uniqueness_of(:name) }
class Foo < ActiveRecord::Base
validates_uniqueness_of :name
end
# spec
describe Foo do
it "should have a unique name" do
Foo.create!(:name=>"Foo")
foo = Foo.new(:name=>"Foo")
foo.should_not be_valid
foo.errors[:name].should include("has already been taken")
end
end
Make it shorter with Shoulda:
it "validates uniqueness of name" do
FactoryGirl.create(:tag, name: 'unique name')
should validate_uniqueness_of(:name)
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With