我学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并检查返回值是什么,我希望它是。然而,这并不工作,永远只是返回“网站名称”。我应该怎么办?

有帮助吗?

解决方案

我不知道,如果这是你的意思,但你可以直接测试的布局:

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

其他提示

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

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

由于呈现被称为控制器的外部,它需要被告知有关布局。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top