Stub/Mock Rails view helpers in RSpec

This was not as easy to find on the inter webs as it should be.

Example

If you want to stub out a view helper (viewhelpermethod_x) in your view spec.

allow(view).to receive(:view_helper_method_x) { return_value_goes_here_as_usual }  

The spec should look something like bellow

# encoding: UTF-8
require 'spec_helper'

describe ‘home/view.html.erb’ do

    before(:each) do
      allow(view).to receive(:view_helper_method_x) { true }
    end

    it ’should do blah blah' do
      render
      expect(rendered).to_not have_css ‘.test_id'
    end
end  

That’s all folks!