Oh, and I suppose the third option here could be to so something like:

[:do_something, :save].each do |method_name|
it "should satisfy some spec when we call the #{method_name} method" do
  x = MyModel.new
  x.call(method_name)
  x.should ... (whatever the condition is here)
end
end

I use this sort of technique at time to keep my specs dry, simple and
behavior-driven at the same time, but I find it can make the spec files a
bit hard to read through so I try not to use it unless it is really
needed...



On Thu, Apr 16, 2009 at 1:25 PM, Barun Singh <baru...@gmail.com> wrote:

> In many of my models, I have callback methods like this:
>
> class MyModel < ActiveRecord::Base
>
>   before_save   :do_something
>
>   def do_something
>     some code here...
>   end
>
> end
>
> The do_something method is a public method that is sometimes called on its
> own, and also called whenever the model is saved.  Let's say there are six
> specs that are required to fully test the do_something method.  What is
> considered best practice for how the before_save filter should be tested?
> If I say that I'm only ever going to test the behavior, it means that I need
> to basically rewrite those six specs to make sure they hold whenever the
> model is saved, as well as when the do_something method is called
> explicitly.  I don't like having to repeat myself this way, particularly
> considering that a model may have multiple callbacks like this so I end up
> having to repeat myself a lot.  The alternative is to just do something
> like:
>
> x = MyModel.new
> x.should_receive(:do_something)
> x.save
>
> But of course this tests implementation rather than behavior which is
> usually not desirable.  But in this situation I'm tending to prefer it over
> having to do a ton of rewriting of specs.  What are others' thoughts on
> this?
>
> Thanks..
>
_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to