trailblazer / rspec-cells

Spec your Cells.
http://cells.rubyforge.org
MIT License
62 stars 48 forks source link

How to stub a view helper method? #25

Closed murphyslaw closed 11 years ago

murphyslaw commented 11 years ago

Hello,

I have a view helper method that is used in almost all of my cells.

class MyCell << Cell::Rails
  helper :my_helper

  def show
    ...
  end

  ...
end

module MyHelper
  def my_helper_method
    ...
  end
end

How can I stub it? I tried

controller.stub(:my_helper_method)

render_cell(:my_cell, :show)

but this doesn't work and I have no clue what to look for to find the object that the method is later called on. Do you have an idea?

fcheung commented 11 years ago

A way seems to be

MyCell.view_context_class.any_instance.stub(...)
murphyslaw commented 11 years ago

Thanks. I will close the issue, as I'm currently not working on it anymore. Your suggestion sounds good.

apotonick commented 11 years ago

Reopening it - I have an idea how to fix that very easily: in the newest Cells release the way cells are constructed was simplified so it should be quite easy provide the cell instance that is then to be rendered. Thanks!

murphyslaw commented 11 years ago

:+1:

apotonick commented 11 years ago

I found out that this is working already! Just do the following:

  it "renders empty posts list" do
    posts = cell(:posts)
    posts.stub(:recent_posts).and_return([])

    posts.render_state(:count).should have_selector("p", :content => "No posts!")
  end

I added this to the README: https://github.com/apotonick/rspec-cells#api

Anyone wants to format this to Markdown? :)