require 'spec_helper' describe Role do before do @role = Role.create(:key => 'teacher', :i18n_variable => {:key => 'teacher', :en => 'Teacher', :zh_tw => 'Teacher in Chinese', :document_class => 'Role'}, :sub_roles => [{:key => 'undergrad', :i18n_variable => {:key => 'yeah', :en => 'Yeah', :zh_tw => 'Yeah', :document_class => 'Role'}, :attribute_fields => [{:key => 'department'}, {:key => 'room'}]}, {:key => 'master', :attribute_fields => [{:key => 'department'}]}]) end describe 'New role' do describe '#Role' do it 'should have :built_in false' do @role.built_in.should be false end it 'should have :disabled false' do @role.disabled.should be false end it 'should have a i18n_variable' do @role.i18n_variable.should be_an_instance_of I18nVariable end it 'should create sub_roles' do @role.should have(2).sub_roles end it 'should create attribute_fields' do @role.sub_roles[1].should have(1).attribute_fields end end describe '#SubRole' do it 'should have :built_in false' do @role.sub_roles[0].built_in.should be false end it 'should have :disabled false' do @role.sub_roles[0].disabled.should be false end it 'should have a i18n_variable' do @role.sub_roles[0].i18n_variable.should be_an_instance_of I18nVariable end end describe '#AttributeField' do it 'should have :built_in false' do @role.sub_roles[0].attribute_fields[0].built_in.should be false end it 'should have :disabled false' do @role.sub_roles[0].attribute_fields[0].disabled.should be false end end end describe 'Edit role' do describe '#Role' do before do @role.update_attributes({:key => 'student', :i18n_variable => {:en => 'Student'}, :sub_roles => [{:key => 'new', :attribute_fields => [{:key => 'bob'}, {:key => 'great'}]}, {:id => @role.sub_roles[0].id}, {:id => @role.sub_roles[1].id}]}) end it 'should not be the old :key' do @role.key.should_not == 'teacher' end it 'should be the new :key' do @role.key.should == 'student' end it 'should not be the old :i18n_variable[:en]' do @role.i18n_variable[:en].should_not == 'Teacher' end it 'should be the new :i18n_variable[:en]' do @role.i18n_variable[:en].should == 'Student' end it 'should have one more SubRole' do @role.should have(3).sub_roles end end end describe 'Destroy' do describe '#AttributeField' do before do @role.update_attributes(:key => 'teacher', :i18n_variable => {:key => 'teacher', :en => 'Teacher', :zh_tw => 'Teacher in Chinese', :document_class => 'Role'}, :sub_roles_attributes => {'0' => {:id => @role.sub_roles[0].id, :key => 'undergrad', :attribute_fields_attributes => {'0' => {:id => @role.sub_roles[0].attribute_fields[0].id, :key => 'department'}, '1' => {:id => @role.sub_roles[0].attribute_fields[1].id, :key => 'room', :_destroy => true}}}}) end it 'should have only one AttributeField for the first SubRole' do @role.sub_roles[0].should have(1).attribute_fields end end describe '#I18nVariable' do before do @role.update_attributes(:key => 'teacher', :i18n_variable_attributes => {:id => @role.i18n_variable.id, :key => 'teacher', :en => 'Teacher', :zh_tw => 'Teacher in Chinese', :document_class => 'Role', :_destroy => true}) end it 'should not have a I18nVariable' do @role.i18n_variable.should_not be_an_instance_of I18nVariable end end describe '#Role' do it 'should destroy the I18nVariable' do id = @role.i18n_variable.id @role.destroy lambda {I18nVariable.find(id)}.should raise_error end end end end