Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec View Tests and content_for :title

I'm trying to test the title tag of my application/layout.html.erb:

<title><%= content_for?(:title) ? yield(:title) : "Foo Bar" %></title>

But I find no way to set the title variable:

it "should show a title" do
  # NameError: `title' is not allowed as an instance variable name
  #view.instance_variable_set(:title, 'Test Title')

  # Deprecation error
  # view.stub(:title) { 'Test Title' }

  # Fail:
  # allow(view).to receive(:title) { 'Test Title' }
  # view.content_for(:title) { 'Test Title' }
  # assign(:title, 'Test Title')
  # helper.stub(:content_for).with(:title).and_return('Test Title')

  render

  expect(rendered).to have_title('Test Title')
end

I'm using Rails 4.1.4, rspec-rails 3.0.2, Capybara 2.4.1. Any suggestions?

like image 380
wintersolutions Avatar asked Oct 22 '25 18:10

wintersolutions


2 Answers

In case you need to test the result of a content_for block, you can access it with

view.content_for(:title)

Not strictly related to the question but useful for similar cases if you end up here googling for this.

like image 90
coorasse Avatar answered Oct 24 '25 12:10

coorasse


You can do something like this

 view.instance_variable_get("@view_flow").set(:title,"Test Title")
 render
 expect(rendered).to have_title('Test Title')
like image 40
Paritosh Piplewar Avatar answered Oct 24 '25 12:10

Paritosh Piplewar