ruby-on-rails - 功能规范和 View 规范之间有什么区别吗?

标签 ruby-on-rails rspec capybara bdd rspec-rails

我有一个关于 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_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

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/

相关文章:

automation - 使用Capybara从文本字段中删除内容

ruby-on-rails - 如何处理rails3中的404/500等错误

ruby-on-rails-3 - 调用 FactoryGirl.create 进行所有 rspec Controller 测试?

ruby - 使用 capybara cucumber 选择多项选择中的所有选项

ruby-on-rails - 为什么我的 rspec 测试失败?

rspec - 如何配置 Poltergeist 或 PhantomJS 不遵循重定向?

ruby - capybara /sinatra 测试的 Rack 错误...似乎没有让 Sinatra 应用程序通过

javascript - Rails 4 嵌套属性 link_to_function

ruby-on-rails - 如何做 rails db :migrate on multiple shards that are not master slave relationship at once on rails?

ruby-on-rails - 如何在 Rails 中使用 "self"关键字