pokemon_spec.rb 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe PKParse::Pokemon, type: :lib do
  4. let(:attrs) do
  5. {
  6. pokedex_number: rand(1..151),
  7. nickname: Faker::Games::Pokemon.name,
  8. raw_nickname: 'raw',
  9. raw_pokemon: 'also raw',
  10. }
  11. end
  12. subject(:pokemon) { described_class.new(attrs) }
  13. describe '.new' do
  14. subject { described_class.new(attrs) }
  15. context 'with valid attributes given' do
  16. it 'sets all attributes' do
  17. hash = subject.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(subject.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(subject.to_h[:foo]).to be_nil
  47. end
  48. end
  49. end
  50. describe '#to_h' do
  51. subject { 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. subject.keys.each do |key|
  58. expect(subject[key]).to eq(pokemon.send(key))
  59. end
  60. end
  61. end
  62. describe '#to_json' do
  63. subject { pokemon.to_json }
  64. specify { expect(pokemon).to receive(:to_h); subject }
  65. end
  66. end