jeudi 25 juin 2015

How to limit test data creation when running two tests with FactoryGirl

I have 48 records being created by FactoryGirl, the records use sequence so that they are all unique.

When I run a test, I get all 48 records being created with names as follow Skill_1 to Skill_48.

When I run a subsequent test, I get an additional 48 records created, these have different new values, Skill_49 to Skill_96

I really want my 2nd test to use the same data set as the first test but cannot figure out how to do so

I've included my code here

# Factory
FactoryGirl.define do
 factory :skill do

  provisioned true

  trait :skill do
   skill true
  end
  trait :language do
   language true
  end
  trait :qualification do
   qualification true
  end
  trait :role do
   role true
  end
  trait :personal_attribute do
   personal_attribute true
    end

  sequence :name do |n|n
   type = '(skill)'

   if language
    type = '(language)'
   end
   if qualification
    type = '(qualification)'
   end
   if role
    type = '(role)'
   end
   if personal_attribute
    type = '(personal_attribute)'
   end

   "skill#{n} #{type}"
  end

 end
end

UNIT TEST is HERE

describe SkillQueryService do

  let(:skills) { create_list(:skill, 10, :skill) }
  let(:languages) { create_list(:skill, 2, :language) }
  let(:qualifications) { create_list(:skill, 3, :qualification) }
  let(:roles) { create_list(:skill, 4, :role) }
  let(:personal_attributes) { create_list(:skill, 5, :personal_attribute) }

  let(:unprovisioned_skills) { create_list(:skill, 10, :skill, :provisioned  => false) }
  let(:unprovisioned_languages) { create_list(:skill, 2, :language, :provisioned  => false) }
  let(:unprovisioned_qualifications) { create_list(:skill, 3, :qualification, :provisioned  => false) }
  let(:unprovisioned_roles) { create_list(:skill, 4, :role, :provisioned  => false) }
  let(:unprovisioned_personal_attributes) { create_list(:skill, 5, :personal_attribute, :provisioned => false) }

  context 'sugguest' do

    it 'returns 20 suggested provisioned skills' do

      # Build TEST data

      service = SkillQueryService.new

      rows = service.suggest('skill')

      # rows.each do |r|
      #   display_skill(r)
      # end
      # THIS CODE PRINTS OUT SKILLS 1-48
      expect(rows.length).to eq(20)

    end


    it 'returns 20 suggested (UN)-provisioned skills' do

      # Build TEST data
      full_data_set

      service = SkillQueryService.new

      rows = service.suggest('skill')

      # rows.each do |r|
      #   display_skill(r)
      # end
      # THIS CODE PRINTS OUT SKILLS 49-96
      # HOW do I get it to have the same data as above, SKILLS 41-48

      expect(rows.length).to eq(20)
    end

  end

  def full_data_set
      skills
      languages
      qualifications
      roles
      personal_attributes

      unprovisioned_skills
      unprovisioned_languages
      unprovisioned_qualifications
      unprovisioned_roles
      unprovisioned_personal_attributes

  end

  def display_skill(skill)
    PL.kv 'name', skill.name
    PL.kv 'provisioned', skill.provisioned
    PL.kv 'skill', skill.skill
    PL.kv 'language', skill.language
    PL.kv 'qualification', skill.qualification
    PL.kv 'role', skill.role
    PL.kv 'personal_attribute', skill.personal_attribute
    PL.line
  end

  def display_skills

    PL.line
    Skill.all.each do |r|
      display_skill(r)
    end
  end

end

Aucun commentaire:

Enregistrer un commentaire