Dhepthi
Dhepthi

Reputation: 453

Can't test render functionality for helper methods in Rspec

I am using Rails 2.3.4 and Rspec 1.2.0. I m trying to test a helper that attempts to render a page or a partial, I'm getting an exception as

undefined method `render' for

Assume, my helper method is

def some_helper
 render(:partial => "some/partial", :locals => {:some => some}
end 

and calling it from spec as

it "should render the partial" do
 some_helper.should render_template("some/partial")
end

Any suggestion would be useful

Upvotes: 7

Views: 2359

Answers (2)

aroaro
aroaro

Reputation: 273

The above solution by Eric C is not working for me. In case anyone is finding the same, here is my working test code:

it 'renders the template' do
  allow(helper).to receive(:render).and_call_original
  helper.helper_method(arg)
  expect(helper).to have_received(:render).with(partial: 'my/partial')
end

Upvotes: 0

Eric C
Eric C

Reputation: 2205

What about:

it "should render the partial" do
  helper.should_receive("render").with("some/partial")
  some_helper
end

UPDATE

When you use the new expectation syntax I would do

it "renders the partial" do
  allow(helper).to receive(:render)
  some_helper
  expect(helper).to have_received(:render).with("some/partial")
end

Upvotes: 8

Related Questions