diff --git a/app/models/gremium.rb b/app/models/gremium.rb index e7c4b1a..05ae095 100644 --- a/app/models/gremium.rb +++ b/app/models/gremium.rb @@ -28,8 +28,11 @@ class Gremium < ActiveRecord::Base text :desc text :name end - + belongs_to :thema # Gehört zu einem Thema + validates :name, :presence=>true + validates :typ, :presence=>true + validates :typ, :inclusion =>{ in: Gremium::TYPEN.keys()} scope :tabs, -> { where(:typ => [1,3]).order(:typ).order(:name) } # Gremien die in Tabs angezeigt werden (Alle Anderen nur in der Liste diff --git a/spec/factories/meetings.rb b/spec/factories/meetings.rb index 5a19282..306f158 100644 --- a/spec/factories/meetings.rb +++ b/spec/factories/meetings.rb @@ -11,7 +11,10 @@ FactoryGirl.define do c.calentry=FactoryGirl.build(:calentry, typ: 2) end - + + trait :with_meetingtyp do + association :meetingtyp, factory: :meetingtyp + end # trait :without_calentry do # calentry nil # end diff --git a/spec/factories/neuigkeit.rb b/spec/factories/neuigkeit.rb index c6b248f..c984d61 100644 --- a/spec/factories/neuigkeit.rb +++ b/spec/factories/neuigkeit.rb @@ -15,7 +15,7 @@ FactoryGirl.define do end trait :with_meeting do after(:build) do |n| - n.meeting=FactoryGirl.build(:meeting) + n.meeting=FactoryGirl.build(:meeting, :with_meetingtyp) end diff --git a/spec/factories/studien.rb b/spec/factories/studien.rb index 5677904..9e82de1 100644 --- a/spec/factories/studien.rb +++ b/spec/factories/studien.rb @@ -10,7 +10,7 @@ FactoryGirl.define do factory :other_studium do name "Telecommunication" - desc "frueher Telekommungikation" + desc "frueher Telekommunikation" zahl "066507" typ "Master" end diff --git a/spec/factories/studium_factory.rb b/spec/factories/studium_factory.rb index 8b13789..e69de29 100644 --- a/spec/factories/studium_factory.rb +++ b/spec/factories/studium_factory.rb @@ -1 +0,0 @@ - diff --git a/spec/models/gremium_spec.rb b/spec/models/gremium_spec.rb index cc6d887..786c9a2 100644 --- a/spec/models/gremium_spec.rb +++ b/spec/models/gremium_spec.rb @@ -1,27 +1,35 @@ require 'spec_helper' describe Gremium do - it "can be created" do - g=FactoryGirl.build(:gremium) - expect(g.save).to be true - end - it "is valid with all attributes" do - g = FactoryGirl.build(:gremium) - g.should be_valid - end + let(:gremium) {FactoryGirl.build(:gremium)} + let(:object_variable){gremium} + it_behaves_like "a valid object" + [:name,:typ].each do |attr| - it "should not be valid without #{attr}" do - g = FactoryGirl.build(:gremium) - g.send("#{attr}=".to_sym,nil) + it "is not valid without #{attr}" do + g = FactoryGirl.build(:gremium,attr.to_sym => nil) expect(g).not_to be_valid expect(g.errors[attr]).to have_at_least(1).items - end end - it "is in tabs if typ is 1 or 3" do + + pending "only accepts valid types" + pending "only accepts valid geschlecht" + + it "is not valid with wrong typ" do + ((-5..30).to_a-Gremium::TYPEN.keys()).each do |i| + g = FactoryGirl.build(:gremium, typ: i) + expect(g).not_to be_valid + end + + end + + it "is in tabs if typ is 1" do g = FactoryGirl.create(:gremium, typ: 1) expect(Gremium.tabs).to eq([g]) end + + it "is not in tabs if typ is not 1 or 3" do g = FactoryGirl.create(:gremium, typ: 2) expect(Gremium.tabs).to eq([]) diff --git a/spec/models/studium_spec.rb b/spec/models/studium_spec.rb index ab9a349..5abc5d0 100644 --- a/spec/models/studium_spec.rb +++ b/spec/models/studium_spec.rb @@ -2,29 +2,45 @@ require 'spec_helper' describe Studium do # test if it is invalid if certain attributes are missing + let(:studium) { FactoryGirl.build(:studium)} + let(:object_variable) {studium} + [:name, :zahl].each do |attr| it "should not be valid without #{attr}" do s = FactoryGirl.build(:studium, attr=>nil) - s.should_not be_valid + expect(s).not_to be_valid end end - it "should create studium with valid data" do - s=FactoryGirl.build(:studium) - lambda { - s.save!; Sunspot.commit}.should change {Studium.count()}.by(1) - end - it "should not accept double entrys" do - FactoryGirl.create(:studium) - Sunspot.commit - s=FactoryGirl.build(:studium) - s.should_not be_valid - s.should have_at_least(1).error_on(:name) - s.should have_at_least(1).error_on(:zahl) - end - it "should expect zahl to be 000.000" do - s=FactoryGirl.build(:studium, :zahl=>"000.000") - Sunspot.commit - s.should_not be_valid + + it_behaves_like "a valid object" + + it "should create studium with valid data" do + s=FactoryGirl.build(:studium) + lambda { + s.save!; Sunspot.commit}.should change {Studium.count()}.by(1) + end + it "should not accept double entrys" do + FactoryGirl.create(:studium) + expect(studium).not_to be_valid + + + expect(studium).to have_at_least(1).error_on(:name) + expect(studium).to have_at_least(1).error_on(:zahl) + end + it "expect zahl to be 000.000" do + s=FactoryGirl.build(:studium, :zahl=>"000.000") + #Sunspot.commit + s.should_not be_valid expect(s.errors.size).to be > 0 - end + end + + it "is not valid with a short zahl" do + s=FactoryGirl.build(:studium, :zahl=>"123") + expect(s).not_to be_valid + end + + it "has a alias title for name" do + expect(studium.title).to eq(studium.name) + end + end