Hi, (Mocha and Stubba are great, btw.)
Is there any way to alias the call to .type in Mocha? The issue is
that AR effectively aliases the normal type method for the Column
class (for whatever Adapter.) My code is reflecting on the adapter
methods to iterate over the columns in a particular model. This makes
it hard to write test code that stubs out columns and mocks my models.
E.g.
cols = [stub(:primary => true, :name => ''id'', :type
=> :integer),
stub(:primary => false, :name => ''desc'', :type
=> :string)]
model = mock
model.stubs(:columns).returns(cols)
Then later:
model.columns.each do |col|
display_type = case col.type
when :integer : ''Int''
when :string : ''String''
else ''n/a''
end
The trouble is that stub in the cols array returns Mocha::Mock as the
type, not what the :type key says it should return. I tried alias
Mocha:Mock :type to :otype in my test and in lib/mocha/mock.rb bot to
no avail. The alias takes, in that s.otype returns "Mocha::Mock" but
the stub method does not really redefine the type method, it stll
returns Mocha::Mock
Ed
--
Ed Howland
http://greenprogrammer.blogspot.com
Sorry - forgot to send to mailing list.
---------- Forwarded message ----------
From: James Mead <jamesmead44 at gmail.com>
Date: 12-Sep-2006 09:20
Subject: Re: [mocha-developer] Aliasing type method in Mocha
To: Ed Howland <ed.howland at gmail.com>
Thanks for the positive feedback. There is a known issue with not being able
to mock the ''type'' method. To date we haven''t found
it much of a problem -
the worst case we found was where we wanted to have a mock as an associated
AR model, but we got round that by stubbing the association methods
themselves.
This is on the todo list and I''ll try to get to it as soon as possible,
but
in the meantime can you do something like...
col = stub(:primary => true, :name => ''id'')
class << col; def type; :integer; end; end
I know it''s not very elegant, but you can probably refactor it into a
method.
--
James.
http://blog.floehopper.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://rubyforge.org/pipermail/mocha-developer/attachments/20060912/b513ef64/attachment.html