如何从 RSpec 视图规范测试页面的标题?

发布于 2024-09-28 19:58:59 字数 406 浏览 2 评论 0原文

我正在学习 RSpec 2 和 Rails 3。为了在每个页面的布局中设置标签的内容,我有一个可用于设置标题然后返回它的帮助程序:

def page_title(subtitle=nil)
  if @title.nil?
    @title = ["Site Name"]
  end

  unless subtitle.nil?
    @title << subtitle
  end

  @title.reverse.join " - "
end

从两个页面调用该帮助程序布局(返回标题)和各个视图(设置标题)。现在,我想在视图规范中测试标题设置是否正确。由于布局未渲染,我决定从规范中调用 page_title 并检查返回值是否符合我的预期。但是,这不起作用,并且始终只返回“站点名称”。我应该怎么办?

I'm learning RSpec 2 with Rails 3. In order to set the contents of the tag in the layout for each page, I have a helper that can be used to set the title and then return it:

def page_title(subtitle=nil)
  if @title.nil?
    @title = ["Site Name"]
  end

  unless subtitle.nil?
    @title << subtitle
  end

  @title.reverse.join " - "
end

The helper is called from both the layout, where it returns the title, and the individual views, where it sets the title. Now, I want to test in the view specs that the title is being set correctly. Since the layout is not rendered, I have decided to call page_title from the spec and check that the return value is what I expect it to be. However, this doesn't work, and always just returns "Site Name". What should I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

唱一曲作罢 2024-10-05 19:58:59

要检查视图规范中页面的标题,请尝试:

require "spec_helper"

describe "controller/view.html.erb" do
  it "renders page title with 'Title | MySite'" do
    render template: "controller/view", layout: "layouts/application"
    rendered.should have_selector("title", text: "Title | MySite")
  end
end

因为渲染是在控制器外部调用的,所以需要告知它有关布局的信息。

To check the title of a page in a view spec try:

require "spec_helper"

describe "controller/view.html.erb" do
  it "renders page title with 'Title | MySite'" do
    render template: "controller/view", layout: "layouts/application"
    rendered.should have_selector("title", text: "Title | MySite")
  end
end

Because render is being called outside of the controller, it needs to be told about the layout.

猛虎独行 2024-10-05 19:58:59

我不确定这是否是您的意思,但您可以直接测试布局:

require 'spec_helper'
include ApplicationHelper

describe "layouts/application" do
  it "should add subtitle to page title" do
    page_title("Subtitle")
    render
    rendered.should have_selector('title:contains("Subtitle - Site Name")')
  end
end

编辑

您还可以测试在视图中调用 page_title 方法:

describe "mycontroller/index" do
  it "should set subtitle" do
    view.should_receive(:page_title).with("Subtitle")
    render
  end
end

或您可以使用 render_views 进行控制器测试:

describe Mycontroller do
  render_views
  it "sets the page title" do
    get :index
    response.body.should contain("Subtitle - Site Name")
  end
end

I'm not sure if this is what you meant, but you could test the layout directly:

require 'spec_helper'
include ApplicationHelper

describe "layouts/application" do
  it "should add subtitle to page title" do
    page_title("Subtitle")
    render
    rendered.should have_selector('title:contains("Subtitle - Site Name")')
  end
end

EDIT

You could also test that the page_title method is called in the view:

describe "mycontroller/index" do
  it "should set subtitle" do
    view.should_receive(:page_title).with("Subtitle")
    render
  end
end

or you could use a controller test with render_views:

describe Mycontroller do
  render_views
  it "sets the page title" do
    get :index
    response.body.should contain("Subtitle - Site Name")
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文