diff --git a/Gemfile b/Gemfile index 14e4945..1f9d575 100644 --- a/Gemfile +++ b/Gemfile @@ -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 diff --git a/Gemfile.lock b/Gemfile.lock index 166dc73..d5c653d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) diff --git a/config/environments/test.rb b/config/environments/test.rb index 93ed4f1..08ff653 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -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 diff --git a/test/controllers/api/pokemons_controller_test.rb b/test/controllers/api/pokemons_controller_test.rb new file mode 100644 index 0000000..891799f --- /dev/null +++ b/test/controllers/api/pokemons_controller_test.rb @@ -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 diff --git a/test/factories/pokemons.rb b/test/factories/pokemons.rb new file mode 100644 index 0000000..f9695a9 --- /dev/null +++ b/test/factories/pokemons.rb @@ -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 diff --git a/test/factories/types.rb b/test/factories/types.rb new file mode 100644 index 0000000..7b8adea --- /dev/null +++ b/test/factories/types.rb @@ -0,0 +1,5 @@ +FactoryBot.define do + factory :type do + name { Faker::Types.rb_string } + end +end diff --git a/test/fixtures/files/.keep b/test/fixtures/files/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/test/test_helper.rb b/test/test_helper.rb index d86c9da..360fc8e 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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)