[rspec-users] object_instance.reload and print out a message placed in the model

Justin Ko jko170 at gmail.com
Mon Feb 21 20:25:28 EST 2011


On Mon, Feb 21, 2011 at 5:18 AM, Daniel Salmeron Amselem <
daniel.amselem at gmail.com> wrote:

> I'm trying to test this with Ruby 1.9.2, RSpec 2.5.0, and Rails 3.0.4:
>
>   context "toggle_resource method" do
>     it "should remove the resource from the group" do
>       group = Factory(:unique_group)
>       user = Factory(:unique_user, :account => group.account)
>
>       group.add_resource(user)
>       group.reload
>       group.should have(1).users
>
>       group.toggle_resource(user)
>       group.reload
>       group.should have(0).users
>     end
>   end
>
> and I had to add 'group.reload' in order to make this thing work. I would
> appreciate if anyone could explain to me why do I need to do it, and why I
> can't see a message that it's in the model, something like the following:
>
>   def add_resource(resource)
>     p "HEY"
>     GroupResource.create(:resource => resource, :group => self)
>   end
>
> The message "HEY" doesn't appear in the console after running 'rspec
> spec/models/group_spec.rb'. Thanks in advance.
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users



You have to call "reload" to clear activerecord caches. Destroying an
associated record does not affect the cache.

One way you could write your spec is like this:

expect { group.add_resource(user) }.to change(group, :users).by(1)
expect { group.toggle_resource(user) }.to change(group, :users).by(-1)

Let us know if that works for you. Thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110221/9c4473d6/attachment.html>


More information about the rspec-users mailing list