From b146deb909df8bde211673a9f2dfd39983a21752 Mon Sep 17 00:00:00 2001 From: Robert Richter Date: Mon, 11 Jul 2016 18:14:29 -0500 Subject: [PATCH] Migrate all tests to Factory_Girl --- .../controllers/characters_controller_test.rb | 37 +++++++++-------- test/controllers/locations_controller_test.rb | 41 ++++++++++--------- test/controllers/universes_controller_test.rb | 26 +++++++----- test/models/character_test.rb | 18 +------- test/models/location_test.rb | 17 +------- test/models/universe_test.rb | 19 +-------- test/test_helper.rb | 2 +- 7 files changed, 63 insertions(+), 97 deletions(-) diff --git a/test/controllers/characters_controller_test.rb b/test/controllers/characters_controller_test.rb index 1265a0be..44f4ced6 100644 --- a/test/controllers/characters_controller_test.rb +++ b/test/controllers/characters_controller_test.rb @@ -6,8 +6,8 @@ class CharactersControllerTest < ActionController::TestCase setup do @request.env["devise.mapping"] = Devise.mappings[:user] - @user = users(:tolkien) - @universe = universes(:middleearth) + @user = create(:user) + @universe = create(:universe, user: @user) sign_in @user end @@ -24,11 +24,13 @@ class CharactersControllerTest < ActionController::TestCase end test 'should create character' do + character = build(:character, universe: @universe, age: 70) + assert_difference('Character.count') do post :create, character: { - age: 'Created Age', - name: 'Created Name', - universe: @universe + age: character.age, + name: character.name, + universe: character.universe } end @@ -36,34 +38,37 @@ class CharactersControllerTest < ActionController::TestCase end test 'should show character' do - @character = characters(:frodo) + character = create(:character, user: @user) - get :show, id: @character.id + get :show, id: character.id assert_response :success end test 'should get edit' do - @character = characters(:frodo) - get :edit, id: @character.id + character = create(:character, user: @user) + + get :edit, id: character.id assert_response :success end test 'should update character' do - @character = characters(:frodo) + character = create(:character, age: 70, universe: @universe, user: @user) - put :update, id: @character, character: { - age: @character.age, - name: @character.name, - universe: @universe + put :update, id: character.id, character: { + age: character.age, + name: character.name, + universe: character.universe } assert_response 302 - assert_redirected_to character_path(@character) + assert_redirected_to character_path(character) end test 'should destroy character' do + character = create(:character, user: @user) + assert_difference('Character.count', -1) do - delete :destroy, id: characters(:frodo).id + delete :destroy, id: character.id end assert_redirected_to characters_url diff --git a/test/controllers/locations_controller_test.rb b/test/controllers/locations_controller_test.rb index d37418e6..2730d197 100644 --- a/test/controllers/locations_controller_test.rb +++ b/test/controllers/locations_controller_test.rb @@ -4,16 +4,11 @@ require 'test_helper' class LocationsControllerTest < ActionController::TestCase include Devise::TestHelpers + setup do @request.env["devise.mapping"] = Devise.mappings[:user] - - @user = users(:tolkien) - sign_in @user - end - setup do - @request.env["devise.mapping"] = Devise.mappings[:user] - @universe = universes(:middleearth) - @user = users(:tolkien) + @user = create(:user) + @universe = create(:universe, user: @user) sign_in @user end @@ -30,9 +25,11 @@ class LocationsControllerTest < ActionController::TestCase end test 'should create location' do + location = build(:location, user: @user) + assert_difference('Location.count') do post :create, location: { - name: 'Isengard', + name: location.name, universe: @universe, user: @user } @@ -42,42 +39,48 @@ class LocationsControllerTest < ActionController::TestCase end test 'should show location' do - @location = locations(:shire) + location = create(:location, user: @user) - get :show, id: @location.id + get :show, id: location assert_response :success end test 'should get edit' do - @location = locations(:shire) - get :edit, id: @location.id + location = create(:location, user: @user) + + get :edit, id: location.id assert_response :success end test 'should update location' do - @location = locations(:shire) - put :update, id: @location, location: { - name: 'Bag End', + location = create(:location, user: @user) + + put :update, id: location, location: { + name: location.name + ' Updated', universe: @universe } assert_response 302 - assert_redirected_to location_path(@location) + assert_redirected_to location_path(location) end test 'should destroy location' do + location = create(:location, user: @user) + assert_difference('Location.count', -1) do - delete :destroy, id: locations(:shire).id + delete :destroy, id: location.id end assert_redirected_to locations_url end test 'should create location with image' do + location = build(:location) + assert_difference('Location.count') do map = fixture_file_upload('mordor_map.jpg', 'image/jpeg') post :create, location: { - name: 'Mordor', + name: location.name, map: map, universe: @universe, user: @user diff --git a/test/controllers/universes_controller_test.rb b/test/controllers/universes_controller_test.rb index c30f442a..ad911fdb 100644 --- a/test/controllers/universes_controller_test.rb +++ b/test/controllers/universes_controller_test.rb @@ -7,7 +7,7 @@ class UniversesControllerTest < ActionController::TestCase setup do @request.env["devise.mapping"] = Devise.mappings[:user] - @user = users(:tolkien) + @user = create(:user) sign_in @user end @@ -23,37 +23,43 @@ class UniversesControllerTest < ActionController::TestCase end test 'should create universe' do + universe = build(:universe, user: @user) + assert_difference('Universe.count') do - post :create, universe: { name: 'Hyrule' } + post :create, universe: { name: universe.name } end assert_redirected_to universe_path(assigns(:content)) end test 'should show universe' do - @universe = universes(:middleearth) + universe = create(:universe, user: @user) - get :show, id: @universe.id + get :show, id: universe.id assert_response :success end test 'should get edit' do - @universe = universes(:middleearth) - get :edit, id: @universe.id + universe = create(:universe, user: @user) + + get :edit, id: universe.id assert_response :success end test 'should update universe' do - @universe = universes(:middleearth) - put :update, id: @universe, universe: { name: 'Arda' } + universe = create(:universe, user: @user) + + put :update, id: universe.id, universe: { name: universe.name + ' Updated' } assert_response 302 - assert_redirected_to universe_path(@universe) + assert_redirected_to universe_path(universe) end test 'should destroy universe' do # MWAHAHAHAHAHA!!!!!!! + universe = create(:universe, user: @user) + assert_difference('Universe.count', -1) do - delete :destroy, id: universes(:middleearth).id + delete :destroy, id: universe.id end assert_redirected_to universes_url diff --git a/test/models/character_test.rb b/test/models/character_test.rb index ab9234a4..9766f4f5 100644 --- a/test/models/character_test.rb +++ b/test/models/character_test.rb @@ -3,24 +3,8 @@ require 'test_helper' # Tests for the model class Character class CharacterTest < ActiveSupport::TestCase test 'character not valid without a name' do - character = characters(:frodo) - character.name = nil + character = build(:character, name: nil) refute character.valid?, 'Character name not being validated for presence' end - - test 'characters fixture assumptions' do - assert_not_nil characters(:frodo), 'Characters fixture is not available' - assert characters(:frodo).valid?, 'Characters fixture is not valid' - - assert_equal( - users(:tolkien), - characters(:frodo).user, - 'Characters fixture is not associated with Users fixture') - - assert_equal( - universes(:middleearth), - characters(:frodo).universe, - 'Characters fixture is not associated with Universes fixture') - end end diff --git a/test/models/location_test.rb b/test/models/location_test.rb index 2fd51f0b..589b986d 100644 --- a/test/models/location_test.rb +++ b/test/models/location_test.rb @@ -3,23 +3,8 @@ require 'test_helper' # Tests for the Location model class class LocationTest < ActiveSupport::TestCase test 'location not valid without a name' do - location = locations(:shire) - location.name = nil + location = build(:location, name: nil) refute location.valid?, 'Location name is not being validated for presence' end - - test 'location fixture assumptions' do - assert_not_nil locations(:shire), - 'Locations fixture not available' - - assert locations(:shire).valid?, - 'Locations fixture not valid' - - assert_equal users(:tolkien), locations(:shire).user, - 'Locations fixture associated with Users fixture' - - assert_equal universes(:middleearth), locations(:shire).universe, - 'Locations fixture not associated with Universes fixture' - end end diff --git a/test/models/universe_test.rb b/test/models/universe_test.rb index 2150673e..2976b1cb 100644 --- a/test/models/universe_test.rb +++ b/test/models/universe_test.rb @@ -3,25 +3,8 @@ require 'test_helper' # Tests for the Universe model class class UniverseTest < ActiveSupport::TestCase test 'universe not valid without a name' do - universe = universes(:middleearth) - universe.name = nil + universe = build(:universe, name: nil) refute universe.valid?, 'Universe name is not being validated for presence' end - - test 'universe fixture assumptions' do - assert_not_nil universes(:middleearth), - 'Universes fixture is not available' - - assert universes(:middleearth).valid?, - 'Universes fixture is not a valid universe' - - assert_equal users(:tolkien), universes(:middleearth).user, - 'Universe fixture not associated with User fixture' - end - - test 'can count content' do - assert_equal 2, universes(:middleearth).content_count, - "Universe didn't count its content properly" - end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 70197766..6f635e19 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -11,7 +11,7 @@ require 'capybara/rails' module ActiveSupport # Helper methods for unit tests class TestCase - fixtures :all + include FactoryGirl::Syntax::Methods def log_in_user(user_fixture) session[:user] = users(user_fixture).id