jam-cloud/ruby/spec/jam_ruby/models/band_spec.rb

91 lines
2.6 KiB
Ruby

require 'spec_helper'
describe Band do
let(:user) { FactoryGirl.create(:user) }
let(:user2) { FactoryGirl.create(:user) }
let(:fan) { FactoryGirl.create(:fan) }
let(:band) { FactoryGirl.create(:band) }
let(:new_band) { FactoryGirl.build(:band) }
let(:band_params) {
{
name: "The Band",
biography: "Biography",
city: 'Austin',
state: 'TX',
country: 'US',
genres: ['country']
}
}
describe 'website update' do
it 'should have http prefix on website url' do
band.website = 'example.com'
band.save!
expect(band.website).to match(/^http:\/\/example.com$/)
end
end
describe 'band validations' do
it "minimum genres" do
new_band.save.should be_false
new_band.errors[:genres].should == [ValidationMessages::BAND_GENRE_MINIMUM_NOT_MET]
end
it "maximum genres" do
new_band.genres = Genre.limit(4)
new_band.save.should be_false
new_band.errors[:genres].should == [ValidationMessages::BAND_GENRE_LIMIT_EXCEEDED]
end
end
describe "save" do
it "can succeed" do
band = Band.save(user, band_params)
band.errors.any?.should be_false
band.name.should == band_params[:name]
band.biography.should == band_params[:biography]
band.genres.should == [Genre.find(band_params[:genres][0])]
band.city.should == band_params[:city]
band.state.should == band_params[:state]
band.country.should == band_params[:country]
end
it "ensures user is a musician" do
expect{ Band.save(fan, band_params) }.to raise_error("must be a musician")
end
it "can update" do
band = Band.save(user, band_params)
band.errors.any?.should be_false
band_params[:id] = band.id
band_params[:name] = "changed name"
band = Band.save(user, band_params)
band.errors.any?.should be_false
Band.find(band.id).name.should == band_params[:name]
end
it "stops non-members from updating" do
band = Band.save(user, band_params)
band.errors.any?.should be_false
band_params[:id] = band.id
band_params[:name] = "changed name"
expect{ Band.save(user2, band_params) }.to raise_error(ValidationMessages::USER_NOT_BAND_MEMBER_VALIDATION_ERROR)
end
end
describe "validate" do
it "can pass" do
band = Band.build_band(user, band_params)
band.valid?.should be_true
end
it "can fail" do
band_params[:name] = nil
band = Band.build_band(user, band_params)
band.valid?.should be_false
band.errors[:name].should == ["can't be blank"]
end
end
end