Created
March 29, 2013 04:42
-
-
Save Catharz/5268789 to your computer and use it in GitHub Desktop.
FactoryGirl issues with attributes_for
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Character < ActiveRecord::Base | |
belongs_to :player, :inverse_of => :characters, :touch => true | |
belongs_to :archetype, :inverse_of => :characters, :touch => true | |
validates_presence_of :name | |
validates_presence_of :player, :archetype, :char_type, :on => :update | |
validates_uniqueness_of :name | |
validates_format_of :char_type, :with => /g|m|r/ # General Alt, Main, Raid Alt | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FactoryGirl.define do | |
sequence :character_name do |n| | |
"Character #{n}" | |
end | |
factory :character do |f| | |
f.player { |a| a.association(:player) } | |
f.name { generate(:character_name) } | |
f.char_type 'g' | |
f.archetype { |a| a.association(:archetype) } | |
end | |
factory :invalid_character, parent: :character do |f| | |
f.player nil | |
f.name nil | |
f.char_type 'f' | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe CharactersController do | |
fixtures :users | |
before(:each) do | |
login_as :foo | |
end | |
describe 'POST #create' do | |
context 'with valid attributes' do | |
it 'saves the new character' do | |
expect { | |
post :create, character: FactoryGirl.attributes_for(:character) | |
}.to change(Character, :count).by(1) | |
end | |
it 'redirects to the new character' do | |
post :create, character: FactoryGirl.attributes_for(:character) | |
response.should redirect_to Character.last | |
end | |
end | |
context 'with invalid attributes' do | |
it 'does not save the new character' do | |
expect { | |
post :create, character: FactoryGirl.attributes_for(:invalid_character) | |
}.to_not change(Character, :count) | |
end | |
it 're-renders the :new template' do | |
post :create, character: FactoryGirl.attributes_for(:invalid_character) | |
response.should render_template :new | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Instance < ActiveRecord::Base | |
belongs_to :raid, inverse_of: :instances, touch: true | |
belongs_to :zone, inverse_of: :instances, touch: true | |
has_many :drops, :inverse_of => :instance, dependent: :destroy | |
has_many :character_instances, :inverse_of => :instance, dependent: :destroy | |
has_many :kills, :through => :drops, :source => :mob, :uniq => true | |
has_many :characters, :through => :character_instances | |
has_many :players, :through => :characters, :uniq => true | |
accepts_nested_attributes_for :character_instances, :reject_if => :all_blank, :allow_destroy => true | |
accepts_nested_attributes_for :drops, :reject_if => :all_blank, :allow_destroy => true | |
validates_presence_of :raid, :zone, :start_time | |
validates_uniqueness_of :start_time, :scope => [:raid_id, :zone_id] | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FactoryGirl.define do | |
sequence :start_time do |n| | |
n.hours | |
end | |
factory :instance do |f| | |
raid_date = Date.parse('01/01/2012') | |
f.raid { |a| a.association(:raid, raid_date: raid_date) } | |
f.zone { |a| a.association(:zone) } | |
f.start_time { raid_date + generate(:start_time) } | |
end | |
factory :invalid_instance, parent: :instance do |f| | |
f.raid nil | |
f.zone nil | |
f.start_time nil | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe InstancesController do | |
fixtures :users | |
before(:each) do | |
login_as :foo | |
end | |
describe 'POST #create' do | |
context 'with valid attributes' do | |
it 'saves the new instance' do | |
expect { | |
post :create, instance: FactoryGirl.build(:instance).attributes.symbolize_keys | |
}.to change(Instance, :count).by(1) | |
end | |
it 'redirects to the new instance' do | |
post :create, instance: FactoryGirl.build(:instance).attributes.symbolize_keys | |
response.should redirect_to Instance.last | |
end | |
end | |
context 'with invalid attributes' do | |
it 'does not save the new instance' do | |
expect { | |
post :create, instance: FactoryGirl.attributes_for(:invalid_instance) | |
}.to_not change(Instance, :count) | |
end | |
it 're-renders the :new template' do | |
post :create, instance: FactoryGirl.attributes_for(:invalid_instance) | |
response.should render_template :new | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The characters factory returns valid association attributes when FactoryGirl.attributes_for(:character) is called.
The instances factory returns null attributes when the above is done, but using FactoryGirl.build(:instance).symbolize_keys does work.