Re: [rspec-users] testing around_save

2012-02-24 Thread apneadiving
Hi David and thanks again,

I run Rspec 2.8.0. and just upgraded from Rails 3.0.10 to Rails 3.2.1.

I must admit, the spec wasn't clean. But it was quick and easy...

Of course I keep your advice in mind, I just have to adapt to my
particular case:
* I work on a legacy and untested app, so factories require much time
to be built
* the callbacks run a lot of code which require I load plenty of data
in the object, a bit overkill just to check whether or not the
callback is triggered
* I run a state machine and I want to be sure proper callbacks are
called (or not) from one step to another, so there are many specs.

I finally decided to redefine the callback content for the current
object, since, in these particular specs, I don't care of what happens
inside.
I had to split my specs in two parts:

context initial status waiting_assignment do

  before(:each) do
document.instance_eval { def on_progress;   yield; end }
  end

   it check callback do
 document.should_receive(:on_progress)
 document.status_progress
   end

   it check transition do
 document.status_progress
 document.status.should eq in_progress
   end

 end


On Feb 24, 2:36 am, David Chelimsky dchelim...@gmail.com wrote:
 On Feb 23, 2012, at 3:36 PM, apneadiving wrote:

  I've just upgraded to Rails 3.2.1 and I can't get my specs checking
  whether or not around_save work anymore.

 What version did you upgrade from? Anything else change in the process?

  I simply did something like:

  object.should_receive :around_filter_name
  object.save.should be_true

  (Actually, it was slightly more complex, it's part of a state machine)

 Mock objects (message expectations / should_receive) are for specifying 
 interaction _between objects_, not internal implementation of a single 
 object. There are plenty of guidelines that point in that direction, two of 
 which are explicitly violated in this example:

 1. mock your own code, not your dependencies (in this case you're mocking 
 ActiveRecord internals)
 2. don't mock the subject of a test

 The motivation for both of these guidelines is exactly what you're 
 experiencing. Something changed out from under you and you have no idea what 
 and why your test doesn't work anymore. I'd recommend using a black block 
 approach for this instead of a message expectation: get the object into the 
 state you want, call object.save, and then specify the resulting state.

 HTH,
 David
 ___
 rspec-users mailing list
 rspec-us...@rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users


Re: [rspec-users] testing around_save

2012-02-23 Thread David Chelimsky
On Feb 23, 2012, at 3:36 PM, apneadiving wrote:

 I've just upgraded to Rails 3.2.1 and I can't get my specs checking
 whether or not around_save work anymore.

What version did you upgrade from? Anything else change in the process?

 I simply did something like:
 
 object.should_receive :around_filter_name
 object.save.should be_true
 
 (Actually, it was slightly more complex, it's part of a state machine)

Mock objects (message expectations / should_receive) are for specifying 
interaction _between objects_, not internal implementation of a single object. 
There are plenty of guidelines that point in that direction, two of which are 
explicitly violated in this example:

1. mock your own code, not your dependencies (in this case you're mocking 
ActiveRecord internals)
2. don't mock the subject of a test

The motivation for both of these guidelines is exactly what you're 
experiencing. Something changed out from under you and you have no idea what 
and why your test doesn't work anymore. I'd recommend using a black block 
approach for this instead of a message expectation: get the object into the 
state you want, call object.save, and then specify the resulting state.

HTH,
David
___
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users