Pat Maddox
2007-Jun-26 19:52 UTC
[rspec-users] Can I stub a method on a belongs_to association:
describe Asset, " when destroyed" do
fixtures :assets, :videos, :sites, :publish_settings
before(:each) do
@asset = assets(:test_asset)
@mock_hook = mock("hook")
@asset.video.stub!(:hook).and_return @mock_hook # error occurs here
end
it "should call the delete hook" do
@mock_hook.should_receive(:update).with("test_video", :asset_id
=>
@assetid, :asset_action => "destroy")
@asset.destroy
end
end
NameError in ''Asset when destroyed should call the delete
hook''
undefined method `hook'' for class
`ActiveRecord::Associations::BelongsToAssociation''
I''m not sure why I can''t stub the hook method on the video
proxy...I
need to though, because my implementation is
video.hook.update....
Any ideas?
Pat
Moses Hohman
2007-Jun-26 20:13 UTC
[rspec-users] Can I stub a method on a belongs_to association:
Stub @asset''s :video first, i.e.
@mock_video_proxy = mock("assay.video proxy")
@asset.stub!(:video).and_return(@mock_video_proxy)
@assay.video.stub!(:hook).and_return @mock_hook
Generally mocking starts to get frustrating when the code you''re
mocking
doesn''t follow the Law of Demeter. I have been creating helper methods
for
these things, e.g.
def mock_video_proxy
@mock_video_proxy ||= begin
proxy = mock("assay.video proxy")
@asset.stub!(:video).and_return(proxy)
proxy
end
proxy
end
so you can just use
mock_video_proxy.stub!(:hook).and_return @mock_hook
in any spec. I think there might be a better way, though. I''m not sure
it''s
worth it to use mocks in this situation since you''re already using
fixtures.
Why not just check the expected outcome of the hook method?
Moses
On 6/26/07, Pat Maddox <pergesu at gmail.com>
wrote:>
> describe Asset, " when destroyed" do
> fixtures :assets, :videos, :sites, :publish_settings
>
> before(:each) do
> @asset = assets(:test_asset)
> @mock_hook = mock("hook")
> @asset.video.stub!(:hook).and_return @mock_hook # error occurs here
> end
>
> it "should call the delete hook" do
> @mock_hook.should_receive(:update).with("test_video",
:asset_id =>
> @assetid, :asset_action => "destroy")
> @asset.destroy
> end
> end
>
> NameError in ''Asset when destroyed should call the delete
hook''
> undefined method `hook'' for class
> `ActiveRecord::Associations::BelongsToAssociation''
>
> I''m not sure why I can''t stub the hook method on the
video proxy...I
> need to though, because my implementation is
>
> video.hook.update....
>
> Any ideas?
> Pat
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://rubyforge.org/pipermail/rspec-users/attachments/20070626/2575700f/attachment.html
Pat Maddox
2007-Jun-26 20:33 UTC
[rspec-users] Can I stub a method on a belongs_to association:
On 6/26/07, Moses Hohman <moses.hohman at gmail.com> wrote:> Stub @asset''s :video first, i.e. > > @mock_video_proxy = mock("assay.video proxy") > @asset.stub!(:video).and_return(@mock_video_proxy) > @assay.video.stub!(:hook).and_return @mock_hook > > Generally mocking starts to get frustrating when the code you''re mocking > doesn''t follow the Law of Demeter. I have been creating helper methods for > these things, e.g. > > def mock_video_proxy > @mock_video_proxy ||= begin > proxy = mock("assay.video proxy") > @asset.stub!(:video).and_return(proxy) > proxy > end > proxy > end > > so you can just use > > mock_video_proxy.stub!(:hook).and_return @mock_hook > > in any spec. I think there might be a better way, though. I''m not sure it''s > worth it to use mocks in this situation since you''re already using fixtures. > Why not just check the expected outcome of the hook method? > > MosesI don''t really want to stub too much since this is kind of an integration test at the model level. I also can''t check the expected outcome of the hook method because it makes a network call. Basically I want to test everything except for the hook method itself, because that''s thoroughly tested elsewhere. So I just need to make sure that it gets called. Pat
Courtenay
2007-Jun-26 23:00 UTC
[rspec-users] Can I stub a method on a belongs_to association:
On 6/26/07, Pat Maddox <pergesu at gmail.com> wrote:> I don''t really want to stub too much since this is kind of an > integration test at the model level. I also can''t check the expected > outcome of the hook method because it makes a network call.What is the actual error?
Pat Maddox
2007-Jun-27 00:26 UTC
[rspec-users] Can I stub a method on a belongs_to association:
On 6/26/07, Courtenay <court3nay at gmail.com> wrote:> On 6/26/07, Pat Maddox <pergesu at gmail.com> wrote: > > I don''t really want to stub too much since this is kind of an > > integration test at the model level. I also can''t check the expected > > outcome of the hook method because it makes a network call. > > What is the actual error? > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >NameError in ''Asset when destroyed should call the delete hook'' undefined method `hook'' for class `ActiveRecord::Associations::BelongsToAssociation''