The Rails controller and views provide a view_context
(usually an ActionView::Base
object) that provides context for generating the views.
A common pattern is to wrap model instances in a Presenter class, in which case the view_context
is usually also passed as an argument so the Presenter can call view methods (e.g. I8n.t()
, Rails path helpers, etc...) as needed.
In my RSpec tests I use a mock to test the view_context
behavior within the Presenter. For the path helpers specifically, I have to mock each path individually:
view_context = ActionView::Base.new
user = UserPresenter.new(FactoryBot.create(:user), view: view_context)
allow(view_context).to receive(:some_custom_path) do |opts|
some_custom_path(opts)
end
Is there an easy way to programmatically mock all paths at once?
I suppose I could loop through the list of paths (not sure how to do that) and mock each one by one, but it feels like not the right approach.
Thanks!
EDIT: Actually the above snippet isn't even correct. It throws an error because the view_context
(ActionView::Base
) doesn't even implement :some_custom_path
in the first place. I'm guessing it's a protection measure against stubbing something that doesn't exist.