[rspec-users] testing sessions

S Ahmed sahmed1020 at gmail.com
Sun Mar 4 21:46:28 UTC 2012


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 <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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20120304/34095b0a/attachment.html>


More information about the rspec-users mailing list