pokemon_spec.rb 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe PKParse::Pokemon, type: :lib do
  4. subject(:pokemon) { described_class.new(attrs) }
  5. let(:attrs) do
  6. {
  7. pokedex_number: rand(1..151),
  8. nickname: Faker::Games::Pokemon.name,
  9. raw_nickname: 'raw',
  10. raw_pokemon: 'also raw',
  11. }
  12. end
  13. describe '.new' do
  14. subject(:parsed_pokemon) { described_class.new(attrs) }
  15. context 'with valid attributes given' do
  16. it 'sets all attributes' do
  17. hash = parsed_pokemon.to_h
  18. hash.keys.each do |key|
  19. expect(hash[key]).to eq(pokemon.send(key))
  20. end
  21. end
  22. end
  23. context 'with missing attributes' do
  24. let(:attrs) do
  25. {
  26. pokedex_number: rand(1..151),
  27. raw_nickname: 'raw',
  28. raw_pokemon: 'also raw',
  29. }
  30. end
  31. it 'ignores the missing attributs' do
  32. expect(parsed_pokemon.nickname).to be_nil
  33. end
  34. end
  35. context 'with invalid attributes given' do
  36. let(:attrs) do
  37. {
  38. pokedex_number: rand(1..151),
  39. nickname: Faker::Games::Pokemon.name,
  40. raw_nickname: 'raw',
  41. raw_pokemon: 'also raw',
  42. foo: 'bar',
  43. }
  44. end
  45. it 'ignores the extra attributs' do
  46. expect(parsed_pokemon.to_h[:foo]).to be_nil
  47. end
  48. end
  49. end
  50. describe '#to_h' do
  51. subject(:pokemon_hash) { pokemon.to_h }
  52. it { is_expected.to have_key(:pokedex_number) }
  53. it { is_expected.to have_key(:nickname) }
  54. it { is_expected.to have_key(:raw_nickname) }
  55. it { is_expected.to have_key(:raw_pokemon) }
  56. it 'sets keys to appropriate values' do
  57. pokemon_hash.keys.each do |key|
  58. expect(pokemon_hash[key]).to eq(pokemon.send(key))
  59. end
  60. end
  61. end
  62. describe '#to_json' do
  63. subject(:pokemon_json) { pokemon.to_json }
  64. specify do
  65. allow(pokemon).to receive(:to_h)
  66. pokemon_json
  67. expect(pokemon).to have_received(:to_h)
  68. end
  69. end
  70. end