功能规范和视图规范之间有什么区别吗?

Is there any difference between a feature spec and a view spec?

我有一个关于水豚内部干燥的问题。汤姆回答得很好,他在回答中提到:

Feature tests should be for testing larger behaviours in the system.

Rails Ruby 中的功能规范和视图规范之间有区别吗?如果可能的话,请用一些例子来解释它。 谢谢。

是的,功能和视图规格完全不同。第一个是完整的集成测试,第二个是单独测试视图。

功能规范 使用无头浏览器从外部测试整个系统,就像用户使用它一样。如果您使用正确的无头浏览器并打开 Javascript.

,它也会执行代码、数据库、视图和 Javascript

与其他类型的 rspec-rails 规范不同,功能规范是使用 featurescenario 方法定义的。

功能规范,并且只有功能规范,使用 Capybara 的所有功能,包括 visitfill_inclick_button 等方法,以及 have_text 等匹配器。

the rspec-rails documentation for feature specs 中有很多示例。这是一个快速的:

feature "Questions" do
  scenario "User posts a question" do
    visit "/questions/ask"
    fill_in "title" with "Is there any difference between a feature spec and a view spec?"
    fill_in "question" with "I had a question ..."
    click_button "Post Your Question"
    expect(page).to have_text "Is there any difference between a feature spec and a view spec?"
    expect(page).to have_text "I had a question"
  end
end

视图规范 仅呈现隔离视图,模板变量由测试而不是控制器提供。

与其他类型的 rspec-rails 规范一样,视图规范是使用 describeit 方法定义的。使用 assign 分配模板变量,使用 render 呈现视图并使用 rendered.

获取结果

视图规范中唯一使用的 Capybara 功能是匹配器,例如 have_text

the rspec-rails documentation of view specs 中有很多示例。这是一个快速的:

describe "questions/show" do
  it "displays the question" do
    assign :title, "Is there any difference between a feature spec and a view spec?"
    assign :question, "I had a question"
    render

    expect(rendered).to match /Is there any difference between a feature spec and a view spec\?/
    expect(rendered).to match /I had a question/
  end
end