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

2024-03-11

我有一个关于水豚干燥度的问题here https://stackoverflow.com/questions/35255786/what-is-the-correct-usage-of-within-method-in-capybara/35257760#35257760。汤姆回答得很完美,他在回答中提到:

功能测试应该用于测试系统中更大的行为。

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


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

A 功能规格使用无头浏览器从外部测试整个系统,就像用户使用它一样。如果您使用正确的无头浏览器并打开 Javascript,它也会练习代码、数据库、视图和 Javascript。

与其他类型的 rspec-rails 规范不同,功能规范是用feature and scenario方法。

功能规格,并且只有功能规格,使用 Capybara 的所有功能,包括visit,方法如fill_in and click_button,和匹配器喜欢have_text.

里面有很多例子有关功能规格的 rspec-rails 文档 https://www.relishapp.com/rspec/rspec-rails/v/3-4/docs/feature-specs/feature-spec。这是一个快速的:

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

A 查看规格只是使用测试而不是控制器提供的模板变量单独渲染视图。

与其他类型的 rspec-rails 规范一样,视图规范是用describe and it方法。一个分配模板变量assign,渲染视图render并得到结果rendered.

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

里面有很多例子查看规范的 rspec-rails 文档 https://www.relishapp.com/rspec/rspec-rails/v/3-4/docs/view-specs/view-spec。这是一个快速的:

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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

功能规格和视图规格之间有什么区别吗? 的相关文章

随机推荐