Calbacks on models

Hi, I’m having some strange behavior on callbacks when testing with
RSpec2. On my test, when I create a resource using the #create method
the callbacks related to #before_create are not called. If I go to the
console and I try to type the command Resource.create(client_uri:
http://example.com”) all works fine.

There are no mocks around, and I can’t understand if this is a normal
behavior using RSpec, if its due to Mongoid or something else.

resource.rb (model)

class Resource
include Mongoid::Document
before_create :random_code, :create_expiration

resource_rspec.rb (testing suite)

let(:resource)  { Resource.create(client_uri: "http://example.com")

Thanks a lot

On Mar 2, 2011, at 11:49 AM, Andrea R. wrote:

class Resource
include Mongoid::Document
before_create :random_code, :create_expiration

resource_rspec.rb (testing suite)

let(:resource) { Resource.create(client_uri: “http://example.com”)

Thanks a lot

There’s nothing in RSpec that implicitly interrupts the normal life
cycle of any model objects. You can do so with stubs, but you have to do
it explicitly.

Is anybody else on this list having a similar experience?

Hi,

Just been looking for answers to this very question:

I have a User model (using devise) and the same problem.

Both the before_create and after_create callbacks fail to run from
within
Rspec2

I can even put invalid code in the callbacks and it is not detected
until I
switch to dev and run through the browser - then it all works as
expected.

rails 3.0.5 and sqlite
rspec (2.5.0)
rspec-core (2.5.1)
rspec-expectations (2.5.0)
rspec-mocks (2.5.0)
rspec-rails (2.5.0)

Has anyone had any thoughts on this as yet ?

Thanks

H

On Mar 26, 2011, at 12:36 PM, Huw N. removed_email_addr[email protected] wrote:

rails 3.0.5 and sqlite
H


rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users

Got code?

OK, found the problem and it’s not rspec - my bad…

Turns out that my spork config using guard etc. does not reload model
files
as it should - looking into that next

Reloading spork (or killing it and running native) and all tests work as
expected.

Sorry for time waste

H

On Sat, Mar 26, 2011 at 8:18 PM, Huw N. [email protected]
wrote:

OK, found the problem and it’s not rspec - my bad…
Turns out that my spork config using guard etc. does not reload model files
as it should - looking into that next
Reloading spork (or killing it and running native) and all tests work as
expected.
Sorry for time waste
H

I’ve seen this same problem with a very similar setup. I believe it’s
got to do with the way devise loads up the user model when routes are
loaded, so basically it’s a problem between spork and devise. (I use
guard as well, but I don’t think it is related.)

I tried:
https://github.com/timcharper/spork/wiki/Spork.trap_method-Jujutsu
With no luck. You might want to take it to the spork mailing list.

On Sun, Mar 27, 2011 at 1:56 PM, Pixel [email protected] wrote:

Have a look at the very latest spork (0.9.0.rc4) on github - I can’t
recall
where I saw it mentioned yesterday but the combo of that devise fix you
linked to and upgrading spork seems to be working for me now…

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs