gpt4 book ai didi

ruby-on-rails - RSpec 查看测试和 content_for :title

转载 作者:行者123 更新时间:2023-12-02 08:32:06 26 4
gpt4 key购买 nike

我正在尝试测试 application/layout.html.erbtitle 标签:

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

但我找不到设置 title 变量的方法:

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

我正在使用 Rails 4.1.4、rspec-rails 3.0.2、Capybara 2.4.1。有什么建议吗?

最佳答案

你可以这样做

 view.instance_variable_get("@view_flow").set(:title,"Test Title")
render
expect(rendered).to have_title('Test Title')

关于ruby-on-rails - RSpec 查看测试和 content_for :title,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25612306/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com