# frozen_string_literal: true require 'rails_helper' RSpec.describe PKParse::Pokemon, type: :lib do subject(:pokemon) { described_class.new(attrs) } let(:attrs) do { pokedex_number: rand(1..151), nickname: Faker::Games::Pokemon.name, raw_nickname: 'raw', raw_pokemon: 'also raw', } end describe '.new' do subject(:parsed_pokemon) { described_class.new(attrs) } context 'with valid attributes given' do it 'sets all attributes' do hash = parsed_pokemon.to_h hash.keys.each do |key| expect(hash[key]).to eq(pokemon.send(key)) end end end context 'with missing attributes' do let(:attrs) do { pokedex_number: rand(1..151), raw_nickname: 'raw', raw_pokemon: 'also raw', } end it 'ignores the missing attributs' do expect(parsed_pokemon.nickname).to be_nil end end context 'with invalid attributes given' do let(:attrs) do { pokedex_number: rand(1..151), nickname: Faker::Games::Pokemon.name, raw_nickname: 'raw', raw_pokemon: 'also raw', foo: 'bar', } end it 'ignores the extra attributs' do expect(parsed_pokemon.to_h[:foo]).to be_nil end end end describe '#to_h' do subject(:pokemon_hash) { pokemon.to_h } it { is_expected.to have_key(:pokedex_number) } it { is_expected.to have_key(:nickname) } it { is_expected.to have_key(:raw_nickname) } it { is_expected.to have_key(:raw_pokemon) } it 'sets keys to appropriate values' do pokemon_hash.keys.each do |key| expect(pokemon_hash[key]).to eq(pokemon.send(key)) end end end describe '#to_json' do subject(:pokemon_json) { pokemon.to_json } specify do allow(pokemon).to receive(:to_h) pokemon_json expect(pokemon).to have_received(:to_h) end end end