← Back to Upcase

FactoryGirl Validation failing


(Charlieanna) #1

This is my validation on a variable

 validates :code, presence: true,uniqueness: true, length: { maximum: 6,minimum: 6 }

and this is the factory

factory :event do
    name "Oracle" 
    sequence(:code) do |n|
        "ABCDE#{n}"
      end
    admin
  end
factory :question do
    body "Hi how are you?"
    approved false
    event
  end

but when i create question 4 times I get

 Validation failed: Code is too long (maximum is 6 characters)

Any reasons how? I thought the value n would go from 0 onwards incrementing.

 scenario "can vote on a question" do
    question = create(:question,approved: true)
    fill_in "subscriber_code",with: question.event.code
    click_button "Join Event"
    page.should have_content question.body
    page.should have_button "up"
  end
  
  scenario "cannot vote on a question that is still pending with the admin" do
    question = create(:question)
    fill_in "subscriber_code",with: question.event.code
    click_button "Join Event"
    page.should_not have_content question.body
    page.should_not have_button "up"
  end
  
  scenario "cannot vote on a question that is has been rejected by admin" do
    question = create(:question)
    fill_in "subscriber_code",with: question.event.code
    click_button "Join Event"
    page.should_not have_content question.body
    page.should_not have_button "up"
  end

(Charlieanna) #2

I started using this for now

factory :event do
    name "Oracle" 
    sequence(:code,10) do |n|
        "ABCD#{n}"
      end
    admin
  end

(Jon Seidel) #3

As I recall, @charlieanna, the sequences generated are three or four characters long?


(Charlieanna) #4

No the sequence n starts from 1 onwards incrementing. My issue was the code had to be 6 letters so when the number n changed from 1 to double digit, the code was becoming 7 letters which was giving validation error. So changed the sequence to start from the double digit 10. So the first one would be “ABCD10”