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?
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.
You can do something like this
view.instance_variable_get("@view_flow").set(:title,"Test Title")
render
expect(rendered).to have_title('Test Title')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With