Skip to content

Commit

Permalink
test: PokemonsController
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienGiboire committed Oct 13, 2021
1 parent e59ef2b commit 6757c92
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ group :test do
gem 'selenium-webdriver'
# Easy installation and use of web drivers to run system tests with browsers
gem 'webdrivers'

gem 'factory_bot_rails'
gem 'faker'
gem 'rexml' # necessary if using ruby 3+
gem 'webmock'
gem 'vcr'
gem 'webmock'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
Expand Down
9 changes: 9 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ GEM
rexml
crass (1.0.6)
erubi (1.10.0)
factory_bot (6.2.0)
activesupport (>= 5.0.0)
factory_bot_rails (6.2.0)
factory_bot (~> 6.2.0)
railties (>= 5.0.0)
faker (2.19.0)
i18n (>= 1.6, < 2)
ffi (1.15.4)
globalid (0.5.2)
activesupport (>= 5.0)
Expand Down Expand Up @@ -216,6 +223,8 @@ DEPENDENCIES
bootsnap (>= 1.4.4)
byebug
capybara (>= 3.26)
factory_bot_rails
faker
jbuilder (~> 2.7)
listen (~> 3.3)
puma (~> 5.0)
Expand Down
2 changes: 2 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,6 @@

# Annotate rendered view with file names.
# config.action_view.annotate_rendered_view_with_filenames = true

config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
end
36 changes: 36 additions & 0 deletions test/controllers/api/pokemons_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require "test_helper"

class Api::PokemonsControllerTest < ActionDispatch::IntegrationTest
def setup
create(:pokemon)
create(:pokemon)
end

test "index responds successfully" do
get api_pokemons_path
assert :success

assert_routing api_pokemons_path, controller: 'api/pokemons', action: 'index'
end

test "index lists all pokemons" do
get api_pokemons_path

assert_equal JSON.parse(response.body).count, Pokemon.count
end

test "show responds successfully" do
get api_pokemon_show_path(1)
assert :success

assert_routing api_pokemon_show_path(1), controller: 'api/pokemons', action: 'show', id: '1'
end

test "show retrieves details of one pokemon" do
pokemon = Pokemon.first

get api_pokemon_show_path(pokemon)

assert_equal JSON.parse(response.body)["id"], pokemon.id
end
end
16 changes: 16 additions & 0 deletions test/factories/pokemons.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FactoryBot.define do
factory :pokemon do
name { Faker::Games::Pokemon.name }
base_experience { Faker::Number.number(digits: 2) }
height { Faker::Number.number(digits: 2) }
is_default { Faker::Boolean.boolean }
order { Faker::Number.number(digits: 2) }
weight { Faker::Number.decimal(l_digits: 2) }

after :create do |pokemon|
types = FactoryBot.create_list :type, 2
pokemon.types << types
pokemon.save
end
end
end
5 changes: 5 additions & 0 deletions test/factories/types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FactoryBot.define do
factory :type do
name { Faker::Types.rb_string }
end
end
Empty file removed test/fixtures/files/.keep
Empty file.
2 changes: 2 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
require 'vcr'

class ActiveSupport::TestCase
include FactoryBot::Syntax::Methods

# Run tests in parallel with specified workers
parallelize(workers: :number_of_processors)

Expand Down

0 comments on commit 6757c92

Please sign in to comment.