[rspec-devel] [ rspec-Bugs-6889 ] Mock expectations not satisfied by code in view

noreply at rubyforge.org noreply at rubyforge.org
Sun Nov 26 18:25:26 EST 2006


Bugs item #6889, was opened at 2006-11-26 04:21
You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3149&aid=6889&group_id=797

Category: mock module
Group: None
>Status: Closed
Resolution: None
Priority: 3
Submitted By: Pat Maddox (pergesu)
Assigned to: Nobody (None)
Summary: Mock expectations not satisfied by code in view

Initial Comment:
Consider this method:

def create_user
  @u = mock("user")
  @u.should_receive(:people).and_return([OpenStruct.new(:name => 'Justin'), OpenStruct.new(:name => 'BJ')])
  User.should_receive(:find_by_login).with('pat').and_return(@u)
end

Now the setup for this spec is:
setup do
  create_user
  get :show, :id => 'pat'
end

Controller code is:
def show
  @user = User.find_by_login params[:id]
end

And then the view:
<% @user.people.each do |p| %>
  <%= h p.name %>
<% end %>

The mock object @u expects the people method to be called.  It is called in the view.  However when I run the spec, I get "Mock 'user' expected :people once, but received it 0 times"

Is this expected behavior?  If so, why?  I haven't found anything in the list archives or previous bug reports.

----------------------------------------------------------------------

Comment By: Pat Maddox (pergesu)
Date: 2006-11-26 16:25

Message:
Alright, I hadn't seen that integrate_views.  Haven't used
rspec in like a week and a half :)

----------------------------------------------------------------------

Comment By: Aslak Hellesøy (aslak_hellesoy)
Date: 2006-11-26 05:44

Message:
As an example of what I'm asking for I have attached a patch created with the following command:

aslak-hellesoys-computer-2:~/scm/rspec/trunk/vendor/rspec_on_rails aslakhellesoy$ svn diff > example_patch.diff

The spec passes - I can't reproduce your problem. I think your spec is a controller spec and if you call integrate_views at the top of your spec I think your spec will pass.



----------------------------------------------------------------------

Comment By: Aslak Hellesøy (aslak_hellesoy)
Date: 2006-11-26 05:26

Message:
If your spec is a controller spec AND you haven't explicitly enabled view integration (using integrate_views) then RSpec will not invoke the view at all (which would explain your failure). This is a feature. For more info see

http://rspec.rubyforge.org/documentation/rails/writing/controllers.html
http://rspec.rubyforge.org/documentation/rails/writing/views.html



----------------------------------------------------------------------

Comment By: Aslak Hellesøy (aslak_hellesoy)
Date: 2006-11-26 05:22

Message:
* What versions/revisions of RSpec/Rails are you using?
* Is this a controller spec or a view spec. Are you using integrated views (http://rspec.rubyforge.org/documentation/rails/writing/controllers.html)? 
* Can you help us reproduce this failure by modifying the demo app in rspec_on_rails and send us a patch file?


----------------------------------------------------------------------

You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=3149&aid=6889&group_id=797


More information about the rspec-devel mailing list