great thanks.
should a var created in a block be reachable outside the block?
it "...." do
expect { session = Session....... }.not_to change(Session, :count)
session.guid.should xxxx
end
I tried that but was a bit confused, whatever is in the expect block is
isolated right?
On Sun, Mar 4, 2012 at 1:34 PM, David Chelimsky <[email protected]>wrote:
> On Sun, Mar 4, 2012 at 7:40 AM, S Ahmed <[email protected]> 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
> [email protected]
> http://rubyforge.org/mailman/listinfo/rspec-users
>
_______________________________________________
rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users