[rspec-users] testing sessions

Zach Dennis zach.dennis at gmail.com
Sun Mar 4 22:15:43 UTC 2012


On Sun, Mar 4, 2012 at 4:46 PM, S Ahmed <sahmed1020 at gmail.com> wrote:
> great thanks.
>
> should a var created in a block be reachable outside the block?

Nope, not unless you defined the variable outside of the block to start with.

>
> 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?

Correct. Currently your session variable is only scoped to the block
that is passed to "expect". Anything outside of that block doesn't
know anything about the session variable.

Zach

>
>
> On Sun, Mar 4, 2012 at 1:34 PM, David Chelimsky <dchelimsky at gmail.com>
> wrote:
>>
>> On Sun, Mar 4, 2012 at 7:40 AM, S Ahmed <sahmed1020 at 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 at rubyforge.org
>> http://rubyforge.org/mailman/listinfo/rspec-users
>
>
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users



-- 

--
@zachdennis
http://www.continuousthinking.com
http://www.mutuallyhuman.com


More information about the rspec-users mailing list