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

Daniel Salmerón Amselem daniel.amselem at gmail.com
Tue Feb 22 12:08:22 EST 2011

Thanks, I'll try that. I just have the feeling ActiveRecord caches are being
a pain in the ass. For example, the toggle_resource method removes or adds a
resource to a group, and when just after that I check the resources on that
group, I still get the same ones before the toggle_resource method was
called. I had to change "self.resources" to "self.resources.all" in order to
force Activerecord to update the cache.

Is there any way I can test this weird behavior? I would love if I could
understand better the way ARel works.

On Tue, Feb 22, 2011 at 2:25 AM, Justin Ko <jko170 at gmail.com> wrote:

> 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.
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
> --
> You received this message because you are subscribed to the Google Groups
> "rspec" group.
> To post to this group, send email to rspec at googlegroups.com.
> To unsubscribe from this group, send email to
> rspec+unsubscribe at googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/rspec?hl=en.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110222/a10aa82a/attachment.html>

More information about the rspec-users mailing list