0

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.

rb-
  • 1
  • 1

1 Answers1

0

Didn't find what's wrong with this particular code, but got around it:

I've used regular expression from here and placed it with format validation defined in Ruby on Rails guide.

# app/models/company.rb
validates :website, format: { with: /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/, message: "invalid url" }
rb-
  • 1
  • 1