I'm trying to test validations on my Rails 4 application. The problem is that adding one specific validation all test passes, regardless if remaining validations are commented or not. Here's my code:
# test/models/company_test.rb
setup do
@company = companies(:one)
@company2 = companies(:two)
end
test "should not save company with invalid name" do
@company.name = ""
assert !@company.save, "Saved company without name"
@company2.name = @company.name
assert !@company2.save, "Saved company without unique name"
@company.name = "a"
assert !@company.save, "Saved company with shorter name than 2 characters"
@company.name = rand(36**65).to_s(36)
assert !@company.save, "Saved company with longer name than 64 characters"
end
test "should not save company if website is invalid" do
@company.website = "foo"
assert !@company.save, "Saved company with invalid website"
end
# app/models/company.rb
validates :name, :owner_id, presence: true, uniqueness: true
validates :name, length: { minimum: 2, maximum: 64 }
validates :website, :format => URI::regexp(%w(http https))
Now if I comment out name validations, all tests still passes. And then if I comment out website validation also, then all tests fail.