0

Given a yml file with

thing1: 'thing1'
thing2: 'thing2'
thing3: 'first'
thing3: 'second'
thing4: 'thing4'
thing5: 'thing5'
thing5: 'thing5'
thing6: 'thing6'

How can I write an rspec test that fails if there are duplicate keys as shown? Duplicate values is ok.

0

Write a test to see if each key (locator name) occurs more than once

require 'rspec'

describe 'Page Objects locator yml file' do
  it 'does not have duplicates' do
    file = File.open('locators.yml')
    pairs = []
    keys = []

    file.each_line do |line|
      words = line.split(': ')
      pairs << {words[0] => words[1]}
      keys << words[0]
    end 
    file.close

    if keys.uniq.count != keys.count
      p "Duplicate Page Object key(s) found: #{keys.uniq.count} unique vs #{keys.count} total"
      p 'dupe keys:'
      dupe_keys = keys.select{|n| keys.count(n) > 1}.uniq
      dupe_keys.each do |key|
        pairs.each do |pair|
          p "#{key.to_sym} : #{pair[key]}" if pair[key]
        end 
      end 
    end 

    expect(keys.uniq.count).to (eq keys.count),
      lambda {"Duplicate page object keys found! #{dupe_keys}"}

  end 
end
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.