# frozen_string_literal: true require 'rails_helper' RSpec.describe PKParse::Pokemon, type: :lib do let(:attrs) do { pokedex_number: rand(1..151), nickname: Faker::Games::Pokemon.name, raw_nickname: 'raw', raw_pokemon: 'also raw', } end subject(:pokemon) { described_class.new(attrs) } describe '.new' do subject { described_class.new(attrs) } context 'with valid attributes given' do it 'sets all attributes' do hash = subject.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(subject.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(subject.to_h[:foo]).to be_nil end end end describe '#to_h' do subject { 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 subject.keys.each do |key| expect(subject[key]).to eq(pokemon.send(key)) end end end describe '#to_json' do subject { pokemon.to_json } specify { expect(pokemon).to receive(:to_h); subject } end end