samedi 31 janvier 2015

rails weird test faliures

I am testing rails using the default testing suite I have a personal model and address associated model. I have first written the personal model then all its test were passing after implementing address model some of the personal test are failing and I have no clue



personal.rb


class Personal < ActiveRecord::Base
belongs_to :applicant
has_many :addresses
validates_associated :addresses

validates :first_name, presence: true, length: { maximum: 24 }
validates :last_name, presence: true, length: { maximum: 24 }
validates :middle_name, length: { maximum: 24 }
validates :fathers_name, presence: true, length: { in: 2..50 }
validates :mothers_name, presence: true, length: { in: 2..50 }
validates :date_of_birth, presence: true
validates :gender, presence: true, inclusion: { in: %w(M F O) }
validates :religion, presence: true, length: { in: 2..20 }
validates :caste, presence: true, inclusion: { in: %w(GEN SC ST OBC_A OBC_B) }
validates :phone, format: { with: /((\+*)((0[ -]+)*|(91 )*)(\d{12}+|\d{10}+))|\d{5}([- ]*)\d{6}|\d{2}([- ]*)\d{7}/ },
length: { maximum: 16 }

validates :mobile, presence: true, format: { with: /(?:(?:\+|0{0,2})91(\s*[\-]\s*)?|[0]?)?[789]\d{9}/ },
length: { maximum: 14 }

accepts_nested_attributes_for :addresses, reject_if: lambda {|attributes| attributes['line1'].blank? }
validate :personal_has_address

private
def personal_has_address
if self.addresses.size == 0
errors.add(:addresses, "There are no address")
end
end

end


and its test as in personal_test.rb



require 'test_helper'

class PersonalTest < ActiveSupport::TestCase
def setup
@personal = personals(:three)
@personal.applicant = applicants(:sourav)
@address = addresses(:three)
@address.personal = @personal
end

test "should be valid" do
assert @personal.valid?
end

test "first name should be present" do
@personal.first_name = " "
assert_not @personal.valid?
end

test "first name should not be too long" do
@personal.first_name = "a" * 25
assert_not @personal.valid?
end

test "last name should be present" do
@personal.last_name = " "
assert_not @personal.valid?
end

test "last name should not be too long" do
@personal.last_name = "a" * 25
assert_not @personal.valid?
end

test "middle name may not be present" do
@personal.middle_name = " "
assert @personal.valid?
end

test "middle name should not be too long" do
@personal.middle_name = "a" * 25
assert_not @personal.valid?
end

test "fathers name may not be present" do
@personal.fathers_name = " "
assert_not @personal.valid?
end

test "fathers name should not be too long" do
@personal.fathers_name = "a" * 51
assert_not @personal.valid?
end

test "fathers name should not be too small" do
@personal.fathers_name = "a"
assert_not @personal.valid?
end

test "mothers name may not be present" do
@personal.mothers_name = " "
assert_not @personal.valid?
end

test "mothers name should not be too long" do
@personal.mothers_name = "a" * 51
assert_not @personal.valid?
end

test "mothers name should not be too small" do
@personal.mothers_name = "a"
assert_not @personal.valid?
end

test "date of birth must be present" do
@personal.date_of_birth = " "
assert_not @personal.valid?
end

test "gender must be present" do
@personal.gender = " "
assert_not @personal.valid?
end

test "gender must be either Male Female or others" do
@personal.gender = "A"
assert_not @personal.valid?
end

test "gender validation should accept valid genders" do
genders = %w(M F O)
genders.each do |gender|
@personal.gender = gender
assert @personal.valid?, "#{gender} is valid gender"
end
end

test "gender validation should reject invalid genders" do
genders = %w(Random A B Male Female Others)
genders.each do |gender|
@personal.gender = gender
assert_not @personal.valid?, "#{gender} is invalid gender"
end
end

test "gender must not be too long" do
@personal.gender = "Ma"
assert_not @personal.valid?
end

test "religion must not be empty" do
@personal.religion = " "
assert_not @personal.valid?
end

test "religion should not be too long" do
@personal.religion = "a" * 21
assert_not @personal.valid?
end

test "religion should not be too short" do
@personal.religion = "a"
assert_not @personal.valid?
end

test "caste validation should reject invalid castes" do
castes = %w(Schedule tribe caste brahmin random Sc tS obc)
castes.each do |caste|
@personal.caste = caste
assert_not @personal.valid?, "#{caste} is invalid caste"
end
end

test "caste validation should accept valid castes" do
castes = %w(GEN SC ST OBC_A OBC_B)
castes.each do |caste|
@personal.caste = caste
assert @personal.valid?, "#{caste} is valid caste"
end
end

test "caste should be present" do
@personal.caste = " "
assert_not @personal.valid?
end

test "phone should not be too long" do
@personal.phone = "1" * 17
assert_not @personal.valid?
end

test "phone validation should accept valid phones" do
phones = ["9775876662", "0-9778545896", "919578965389", "03595-259506", "03598245785", "0 9754845789", "+91 9456211568", "03592 245902"]
phones.each do |phone|
@personal.phone = phone
assert @personal.valid?, "#{phone} is valid phone"
end
end

test "phone validation should reject invalid phones" do
phones = ["1", "033", "97758", "0-9+knv", "random", "0", "12345", "845789", "+91", "033 24028485abcdjj"]
phones.each do |phone|
@personal.phone = phone
assert_not @personal.valid?, "#{phone} is invalid phone"
end
end

test "mobile no should nit be too long" do
@personal.mobile = "1" * 15
assert_not @personal.valid?
end

test "mobile validation should accept valid mobiles" do
mobiles = ["9775876662", "919578965389", "+91-9456211568", ]
mobiles.each do |mobile|
@personal.mobile = mobile
assert @personal.valid?, "#{mobile} is valid mobile"
end
end

test "mobile validation should reject invalid mobiles" do
mobiles = ["1", "033", "97758", "0-9+knv", "random", "0", "12345", "845789", "+91", "033 24028485abcdjj"]
mobiles.each do |mobile|
@personal.mobile = mobile
assert_not @personal.valid?, "#{mobile} is invalid mobile"
end
end

end


and address.rb



class Address < ActiveRecord::Base
belongs_to :personal, dependent: :destroy

validates :line1, presence: true, length: { in: 5..100 }
validates :line2, length: { maximum: 100 }
validates :city_village, presence: true, length: { maximum: 49}
validates :state, presence: true, length: { maximum: 29}
validates :country, presence: true, length: { maximum: 29}
validates :pin, presence: true, numericality: { only_integer: true }
end


Test results



Run options: --seed 12821

# Running:

...FFF..F.......FF...........................

Finished in 0.420629s, 106.9826 runs/s, 180.6817 assertions/s.

1) Failure:
PersonalTest#test_middle_name_may_not_be_present [/home/smoitra/projects/admission/test/models/personal_test.rb:37]:
Failed assertion, no message given.


2) Failure:
PersonalTest#test_caste_validation_should_accept_valid_castes [/home/smoitra/projects/admission/test/models/personal_test.rb:138]:
GEN is valid caste


3) Failure:
PersonalTest#test_phone_validation_should_accept_valid_phones [/home/smoitra/projects/admission/test/models/personal_test.rb:156]:
9775876662 is valid phone


4) Failure:
PersonalTest#test_mobile_validation_should_accept_valid_mobiles [/home/smoitra/projects/admission/test/models/personal_test.rb:177]:
9775876662 is valid mobile


5) Failure:
PersonalTest#test_should_be_valid [/home/smoitra/projects/admission/test/models/personal_test.rb:12]:
Failed assertion, no message given.


6) Failure:
PersonalTest#test_gender_validation_should_accept_valid_genders [/home/smoitra/projects/admission/test/models/personal_test.rb:94]:
M is valid gender

45 runs, 76 assertions, 6 failures, 0 errors, 0 skips
45 tests
76 assertions, 6 failures, 0 errors

106.86 tests/s, 180.47 assertions/s

Finished in 0.4211 seconds

Aucun commentaire:

Enregistrer un commentaire