Skip to content

Commit

Permalink
get latest from railsbridge masteR
Browse files Browse the repository at this point in the history
  • Loading branch information
wholenotes committed May 7, 2012
2 parents 4e59a8c + 0beaefe commit 46a3654
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 154 deletions.
93 changes: 44 additions & 49 deletions spec/controllers/events_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
require 'spec_helper'

describe EventsController do
before do
@event = create(:event)
end

describe "volunteer" do
before do
@event = Factory(:event)
end

context "without logging in, I am redirected from the page" do
it "redirects to the events page" do
Expand All @@ -19,71 +20,67 @@
end
end

context "there is already a rsvp for the volunteer/event" do
#the user may have canceled, changed his/her mind, and decided to volunteer again
context "user is logged in" do
before do
@user = Factory(:user)
@user = create(:user)
sign_in @user

@rsvp = VolunteerRsvp.create(:user_id => @user.id, :event_id => @event.id, :attending => false)
end

it "changes the attending attribute on the rsvp to true" do
get :volunteer, {:id => @event.id}
@rsvp.reload.attending.should == true
end
context "there is already a rsvp for the volunteer/event" do
#the user may have canceled, changed his/her mind, and decided to volunteer again
before do
@rsvp = VolunteerRsvp.create(:user_id => @user.id, :event_id => @event.id, :attending => false)
end

it "does not create any new rsvps" do
expect {
it "changes the attending attribute on the rsvp to true" do
get :volunteer, {:id => @event.id}
}.to_not change { VolunteerRsvp.count }
end
@rsvp.reload.attending.should == true
end

it "does not create any new rsvps" do
expect {
get :volunteer, {:id => @event.id}
}.to_not change { VolunteerRsvp.count }
end

it "redirects to the event page related to the rsvp" do
get :volunteer, {:id => @event.id}
response.should redirect_to(event_path(@event))
end
it "redirects to the event page related to the rsvp" do
get :volunteer, {:id => @event.id}
response.should redirect_to(event_path(@event))
end

it "flashes a confirmation" do
get :volunteer, {:id => @event.id}
flash[:notice].should match(/Thanks for volunteering!/i)
it "flashes a confirmation" do
get :volunteer, {:id => @event.id}
flash[:notice].should match(/Thanks for volunteering!/i)
end
end
end

context "there is no rsvp for the volunteer/event" do
before do
@user = Factory(:user)
sign_in @user
end
context "there is no rsvp for the volunteer/event" do

it "should create a new rsvp, with the the right attributes" do
expect {
get :volunteer, {:id => @event.id}
}.to change { VolunteerRsvp.count }.by(1)
end
it "should create a new rsvp, with the the right attributes" do
expect {
get :volunteer, {:id => @event.id}
}.to change { VolunteerRsvp.count }.by(1)
end

it "redirects to the event page related to the rsvp" do
get :volunteer, {:id => @event.id}
response.should redirect_to(event_path(@event))
end
it "redirects to the event page related to the rsvp" do
get :volunteer, {:id => @event.id}
response.should redirect_to(event_path(@event))
end

it "should flash a confirmation" do
get :volunteer, {:id => @event.id}
flash[:notice].should match(/thanks/i)
it "should flash a confirmation" do
get :volunteer, {:id => @event.id}
flash[:notice].should match(/thanks/i)
end
end
end
end

describe "unvolunteer" do
before do
@event = Factory(:event)
@user = create(:user)
sign_in @user
end

context "there is no rsvp record for this user at this event" do
before do
@user = Factory(:user)
sign_in @user
end

it "should notify the user s/he has not signed up to volunteer for the event" do
get :unvolunteer, {:id => @event.id}
Expand All @@ -93,8 +90,6 @@

context "the user has signed up to volunteer and changed his/her mind" do
before do
@user = Factory(:user)
sign_in @user
@rsvp = VolunteerRsvp.create(:user_id => @user.id, :event_id => @event.id, :attending => true)
end

Expand Down
30 changes: 16 additions & 14 deletions spec/factories.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
Factory.define :user do |f|
f.name "Anne"
f.sequence(:email) { |n| "example0#{n}@example.com"}
f.confirmed_at DateTime.now # all users are confirmed by default
f.password "test123"
end
FactoryGirl.define do
factory :user do
name "Anne"
sequence(:email) { |n| "example0#{n}@example.com"}
confirmed_at DateTime.now # all users are confirmed by default
password "test123"
end

Factory.define :event do |e|
e.sequence(:title) { |n| "Event #{n}" }
e.date DateTime.now
end
factory :event do
sequence(:title) { |n| "Event #{n}" }
date DateTime.now
end

Factory.define :location do |l|
l.sequence(:name) { |n| "Location #{n}" }
l.sequence(:address) { |n| "#{n} Street San Francisco, CA 94108" }
end
factory :location do
sequence(:name) { |n| "Location #{n}" }
sequence(:address) { |n| "#{n} Street San Francisco, CA 94108" }
end
end
149 changes: 72 additions & 77 deletions spec/models/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,108 +3,103 @@
describe Event do

it "must have a title" do
event = Factory.build(:event, :title => nil)
event = build(:event, :title => nil)
event.should_not be_valid
end

it "must have a date" do
event = Factory.build(:event, :date => nil)
event = build(:event, :date => nil)
event.should_not be_valid
end

describe "volunteer rsvps role" do
describe "for existing event" do
before do
@event = Factory(:event)
@event = create(:event)
end

it "should have a volunteer_rsvp method" do
@event.should respond_to(:volunteer_rsvps)
end
end

describe "volunteer" do
before do
@event = Factory(:event)
@user = Factory(:user)

describe "volunteer rsvps role" do
it "should have a volunteer_rsvp method" do
@event.should respond_to(:volunteer_rsvps)
end
end

describe "with existing user" do
before do
@user = create(:user)
end

describe "volunteer" do

it "should have a volunteer method" do
@event.should respond_to(:volunteer!)
end
it "should have a volunteer method" do
@event.should respond_to(:volunteer!)
end

it "should not create duplicate volunteer_rsvps" do
@event.volunteer!(@user)
it "should not create duplicate volunteer_rsvps" do
@event.volunteer!(@user)

#this method is useful for detecting if there is actually a new duplicate record generated in the database
expect {
@event.volunteer!(@user)
}.should_not change(VolunteerRsvp, :count).by(1)
#this method is useful for detecting if there is actually a new duplicate record generated in the database
expect {
@event.volunteer!(@user)
}.should_not change(VolunteerRsvp, :count).by(1)

#this method is useful for detecting when VolunteerRSVP model validation breaks
duplicate_volunteer_rsvp = VolunteerRsvp.new(:user_id => @user.id, :event_id => @event.id, :attending => true)
duplicate_volunteer_rsvp.should_not be_valid
#this method is useful for detecting when VolunteerRSVP model validation breaks
duplicate_volunteer_rsvp = VolunteerRsvp.new(:user_id => @user.id, :event_id => @event.id, :attending => true)
duplicate_volunteer_rsvp.should_not be_valid

end
end

it "should create a volunteer_rsvp" do
expect {
@event.volunteer!(@user)
}.should change(VolunteerRsvp, :count).by(1)
end
it "should create a volunteer_rsvp" do
expect {
@event.volunteer!(@user)
}.should change(VolunteerRsvp, :count).by(1)
end

it "should create a volunteer_rsvp that persists and is valid" do
@rsvp = @event.volunteer!(@user)
@rsvp.should be_persisted
@rsvp.should be_valid
end
it "should create a volunteer_rsvp that persists and is valid" do
@rsvp = @event.volunteer!(@user)
@rsvp.should be_persisted
@rsvp.should be_valid
end

it "should give the new volunteer_rsvp with correct attributes" do
@rsvp = @event.volunteer!(@user)
@rsvp.user_id.should == @user.id
@rsvp.event_id.should == @event.id
@rsvp.attending.should == true
end
end
it "should give the new volunteer_rsvp with correct attributes" do
@rsvp = @event.volunteer!(@user)
@rsvp.user_id.should == @user.id
@rsvp.event_id.should == @event.id
@rsvp.attending.should == true
end
end

describe "unvolunteer" do
before do
@event = Factory(:event)
@user = Factory(:user)
end

it "should have an unvolunteer method" do
@event.should respond_to(:unvolunteer!)
end
describe "unvolunteer" do

it "should have an unvolunteer method" do
@event.should respond_to(:unvolunteer!)
end

it "should change the attending attribute to false" do
@rsvp = @event.volunteer!(@user)
@rsvp.attending.should == true
it "should change the attending attribute to false" do
@rsvp = @event.volunteer!(@user)
@rsvp.attending.should == true

@event.unvolunteer!(@user)
changedStatus = @event.volunteer_rsvps.find_by_user_id(@user.id).attending
changedStatus.should == false
end
end
@event.unvolunteer!(@user)
changedStatus = @event.volunteer_rsvps.find_by_user_id(@user.id).attending
changedStatus.should == false
end
end

describe "volunteering?" do
before do
@event = Factory(:event)
@user = Factory(:user)
end
describe "volunteering?" do
it "should have a volunteering? method" do
@event.should respond_to(:volunteering?)
end

it "should have a volunteering? method" do
@event.should respond_to(:volunteering?)
end
it "should be true when a user is volunteering at an event" do
@event.volunteer!(@user)
@event.volunteering?(@user).should == true
end

it "should be true when a user is volunteering at an event" do
@event.volunteer!(@user)
@event.volunteering?(@user).should == true
end
it "should be false when a user is not volunteering at an event" do
@event.volunteering?(@user).should == false
end

it "should be false when a user is not volunteering at an event" do
@event.volunteering?(@user).should == false
end
end

end

end
2 changes: 1 addition & 1 deletion spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe User do

before { @user = Factory :user, :name=>"Anne" }
before { @user = create(:user, :name=>"Anne") }

subject { @user }

Expand Down
Loading

0 comments on commit 46a3654

Please sign in to comment.