是的,功能和视图规格有很大不同。第一个是完整的集成测试,第二个是单独的视图测试。
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