Migrate all tests to Factory_Girl

This commit is contained in:
Robert Richter 2016-07-11 18:14:29 -05:00
parent 1689acb7db
commit b146deb909
No known key found for this signature in database
GPG Key ID: BEC39BF873A0103B
7 changed files with 63 additions and 97 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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