On Sun, Mar 4, 2012 at 7:40 AM, S Ahmed <sahmed1...@gmail.com> wrote:
> I want to test if my sessions logic works.
>
> Session:
>  id
>  user_id
>
> When I create a new session, if there was a previous session row in the db
> with user_id = xxx, it should delete it first, then create a new row.
>
> How could I test this scenerio?
>
> So far I have:
>
> require 'spec_helper'
>
> describe Session do
>   let(:session) { FactoryGirl.create(:session) }
>   subject { session }
>   it { should be_valid }
>
>   describe "a new session" do
>     s1 = FactoryGirl.build(:session)
>     s2 = FactoryGirl.build(:session)
>     user = FactoryGirl.create(:user)
>
>     s1.user_id = user.id
>     s1.save!
>     #should change(Session, :count).by(1)
>   end
> end
>
> I can't seem to figure out how to use the "should change Session count by
> 1".

First - read the docs at
http://rubydoc.info/gems/rspec-expectations/RSpec/Matchers:change to
learn how to use the `change` matcher properly. Also look at
http://rubydoc.info/gems/rspec-expectations/RSpec/Matchers:expect so
you'll understand my suggestion below.

Second - you say above "When I create a new session, if there was a
previous session row in the db with user_id = xxx, it should delete it
first, then create a new row." This suggests that you want
Session.count _not_ to change at all:

user = FactoryGirl(:user)
session = FactoryGirl(:session, :user_id => user.id)
expect { session.save! }.not_to change(Session, :count)

HTH,
David
_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to