[rspec-users] RSpec 2 view specs: partial mocks

Michael Schuerig michael at schuerig.de
Tue Jun 22 09:27:09 EDT 2010


On Sunday 20 June 2010, Michael Schuerig wrote:
> Are partial mocks supposed to work in rails-rspec 2.0.0.beta.12?
> 
> I'm trying to do things like
> 
>   view.stub(:current_user).and_return(@user)
>   view.stub(:current_page?).and_return(false)
> 
> However, when I do that, the view uses its "ordinary" functionality,
> in  particular, assigns are no longer available as instance
> variables.

I've found the reason:

rspec-
rails-2.0.0.beta.12/lib/rspec/rails/monkey/action_view/test_case.rb

  def _view
    @_view ||= begin
      view = ActionView::Base.new(ActionController::Base.view_paths,
                                  _assigns, @controller)
      ...
      view
    end
  end

#_view (and #view in turn) creates just one ActionView::Base instance 
the first time it is called. That's as it is intended. The effect is, 
however, that only assigns up to that point are passed to that instance. 
Consequently, later assigns are ignored.

Michael

-- 
Michael Schuerig
mailto:michael at schuerig.de
http://www.schuerig.de/michael/


More information about the rspec-users mailing list