[mocha-developer] Stubbing ActiveRecord Models gets very difficult with instance methods

Francois Beausoleil francois.beausoleil at gmail.com
Thu Jan 4 09:51:42 EST 2007

Hello John,

2007/1/3, John Pywtorak <jpywtora at calpoly.edu>:
> Second try with minor correction, doh!
> require 'test/unit'
> require 'date'
> require 'fear'
> class ModelsTest < Test::Unit::TestCase
>     attr :stat
>     def setup
>       @stat = stub(:table_name => "P_TB", :maint_dt => Date.today,
> :maint_time => "00:00")
>     end
>     def test_check
>       StatsTb.stubs(:find_by_table_name).returns(stat)
>       StatsTb.find_stats.each { |st| assert st.check_stat }
>     end
> end
> So, this does not work, because the blocks st object is not an instance
> a real instance with a check method and I can't stub check, because it
> is the method under test.

What's wrong with doing this:

def test_check
  @stat = StatTb.new
  assert @stat.check_stat

Or even:

def test_check
  @stat = StatTb.new(:maint_dt => Date.today, :maint_time => "00:00")
  assert @stat.check_stat

There is not even any need to stub anything in the second version.
You'll get one DB hit, to get the columns, but after that, it should
be connection free.

I hope I understood your problem correctly.

Bye !
François Beausoleil

More information about the mocha-developer mailing list