我有一个关于 capybara 内干燥的问题 here .汤姆回答得很好,在他的回答中他提到:
Feature tests should be for testing larger behaviours in the system.
Ruby on Rails 中的功能规范和 View 规范之间有区别吗?如果可能的话,请用一些例子解释一下。
谢谢你。
最佳答案
是的,功能和 View 规范完全不同。第一个是完整的集成测试,第二个是单独测试 View 。
A 功能规范 使用 headless 浏览器从外部测试整个系统,就像用户使用它一样。如果您使用正确的 headless 浏览器并打开 Javascript,它也会练习代码、数据库、 View 和 Javascript。
与其他类型的 rspec-rails 规范不同,特性规范是用 feature
定义的。和 scenario
方法。
功能规范,并且仅功能规范,使用 Capybara 的所有功能,包括 visit
, 方法如 fill_in
和 click_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
A 查看规范 只是单独呈现一个 View ,模板变量由测试提供,而不是由 Controller 提供。
与其他类型的 rspec-rails 规范一样, View 规范由
describe
定义。和 it
方法。一种使用 assign
分配模板变量, 使用 render
呈现 View 并使用 rendered
获得结果.View 规范中唯一使用的 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
关于ruby-on-rails - 功能规范和 View 规范之间有什么区别吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35258592/