Rspec and state_machine #163

Closed
popdevelop opened this Issue Feb 21, 2012 · 10 comments

Comments

Projects
None yet
6 participants
@ghost

ghost commented Feb 21, 2012

I have the following state machine

class Test < ActiveRecord::Base
  def end_call
    # Called twice
  end

  state_machine :state, :initial => :brand_new do
    after_transition :to => :end, :do => :end_call

    event :start do
      transition :start => :end
    end
  end
end

and the following test in rspec

it "should test the test" do
  Test = Factory(:test)
  test.start!
end

What happens is that end_call gets called twice. Do know its probably not an issue here. But googled everywhere and couldnt find an answer. Probably more people having the same problem as med out there.

@ghost ghost assigned obrie Feb 21, 2012

@obrie

This comment has been minimized.

Show comment Hide comment
@obrie

obrie Feb 21, 2012

Member

Hi @popdevelop -

This will happen if you evaluate the class more than once. Can you put a puts statement before your Test class and validate that you're only seeing the class evaluated once?

Member

obrie commented Feb 21, 2012

Hi @popdevelop -

This will happen if you evaluate the class more than once. Can you put a puts statement before your Test class and validate that you're only seeing the class evaluated once?

@ghost

This comment has been minimized.

Show comment Hide comment
@ghost

ghost Feb 21, 2012

Yes you are correct the Class is evaluated twice. I guess I have to find out why. Thanks for helping me out.

ghost commented Feb 21, 2012

Yes you are correct the Class is evaluated twice. I guess I have to find out why. Thanks for helping me out.

@obrie obrie closed this Feb 21, 2012

@ghost

This comment has been minimized.

Show comment Hide comment
@ghost

ghost Feb 21, 2012

Seems like my problem is that I am using spork and reloads the models every iteration however I am using ActiveSupport::Dependencies.clear to get rid of the dependencies for ActiveSupport can I do something equivalent for state_machine. Thanks for your support.

ghost commented Feb 21, 2012

Seems like my problem is that I am using spork and reloads the models every iteration however I am using ActiveSupport::Dependencies.clear to get rid of the dependencies for ActiveSupport can I do something equivalent for state_machine. Thanks for your support.

@ghost

This comment has been minimized.

Show comment Hide comment
@ghost

ghost Mar 7, 2012

My question is, is there a way to remove the already loaded state_machine since I need to reload the Class without having the callback beeing triggered twice.

ghost commented Mar 7, 2012

My question is, is there a way to remove the already loaded state_machine since I need to reload the Class without having the callback beeing triggered twice.

@obrie

This comment has been minimized.

Show comment Hide comment
@obrie

obrie Mar 7, 2012

Member

Ah, sorry I didn't realize you were asking a question in your previous comment. There's no support way for clearing out the callbacks for a state machine. You can certainly dig your way into the StateMachine::Machine instance, but I wouldn't recommend that.

I'd recommend taking a look at issue #67 where I've addressed this before. You'll see near the end of that issue that the real problem had to do with how Spork was being configured for that particular user. The conversation spilled over to here as well where a solution to the Spork issue was proposed and worked for everyone involved: https://github.com/mongoid/mongoid/issues/1752

Member

obrie commented Mar 7, 2012

Ah, sorry I didn't realize you were asking a question in your previous comment. There's no support way for clearing out the callbacks for a state machine. You can certainly dig your way into the StateMachine::Machine instance, but I wouldn't recommend that.

I'd recommend taking a look at issue #67 where I've addressed this before. You'll see near the end of that issue that the real problem had to do with how Spork was being configured for that particular user. The conversation spilled over to here as well where a solution to the Spork issue was proposed and worked for everyone involved: https://github.com/mongoid/mongoid/issues/1752

@simmogs

This comment has been minimized.

Show comment Hide comment
@simmogs

simmogs Mar 8, 2012

+1 I'm getting the same problem with and without spork.

simmogs commented Mar 8, 2012

+1 I'm getting the same problem with and without spork.

@maxjustus

This comment has been minimized.

Show comment Hide comment
@maxjustus

maxjustus Mar 20, 2012

me too :((

me too :((

@dwilkie

This comment has been minimized.

Show comment Hide comment
@dwilkie

dwilkie Mar 31, 2012

Hey @obrie I tried to look at your solution here: https://github.com/obrie/Issue-illustration/commit/aa9562011c5db4d4923de79942b8762fcebb1228

But it seems like you have removed that repo. Can you post your solution in this issue?

Cheers

dwilkie commented Mar 31, 2012

Hey @obrie I tried to look at your solution here: https://github.com/obrie/Issue-illustration/commit/aa9562011c5db4d4923de79942b8762fcebb1228

But it seems like you have removed that repo. Can you post your solution in this issue?

Cheers

@dwilkie

This comment has been minimized.

Show comment Hide comment
@dwilkie

dwilkie Mar 31, 2012

To get this to work with Spork and RSpec I did the following:
Removed this line from my spec helper (which I added to get spork to reload the classes on each run...)
Dir[Rails.root.join("app/**/*.rb")].each { |f| load f }

And following the guide here: http://my.rails-royce.org/2012/01/14/reloading-models-in-rails-3-1-when-usign-spork-and-cache_classes-true/ I added the following line to my Spork.prefork block before this line require File.expand_path("../../config/environment", __FILE__)

  # Prevent main application to eager_load in the prefork block (do not load files in autoload_paths)
  require 'rails/application'
  Spork.trap_method(Rails::Application, :eager_load!)

Which enables spork to load the files on each run again

dwilkie commented Mar 31, 2012

To get this to work with Spork and RSpec I did the following:
Removed this line from my spec helper (which I added to get spork to reload the classes on each run...)
Dir[Rails.root.join("app/**/*.rb")].each { |f| load f }

And following the guide here: http://my.rails-royce.org/2012/01/14/reloading-models-in-rails-3-1-when-usign-spork-and-cache_classes-true/ I added the following line to my Spork.prefork block before this line require File.expand_path("../../config/environment", __FILE__)

  # Prevent main application to eager_load in the prefork block (do not load files in autoload_paths)
  require 'rails/application'
  Spork.trap_method(Rails::Application, :eager_load!)

Which enables spork to load the files on each run again

@pixelvitamina

This comment has been minimized.

Show comment Hide comment
@pixelvitamina

pixelvitamina Apr 26, 2012

Thanks, this saved my day :-)))

Thanks, this saved my day :-)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment