本文介绍了功能规格和视图规格之间有什么区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I had a question about within dryness in Capybara here. Tom answered perfectly and in his answer he mentioned:

Ruby on Rails中的功能规范和视图规范之间有区别吗?如果可能的话请举例说明.谢谢.

Is there a difference between a feature spec and a view spec in Ruby on Rails? If possible explain it with some example please.Thank you.

推荐答案

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

Yes, feature and view specs are quite different. The first is a full integration test and the second tests a view in isolation.

功能规格使用无头浏览器从外部测试整个系统,就像用户使用它一样.如果您使用正确的无头浏览器并打开Javascript,它还将锻炼代码,数据库,视图和Javascript.

A feature spec uses a headless browser to test the entire system from the outside just like a user uses it. It exercises code, database, views and Javascript too, if you use the right headless browser and turn on Javascript.

与其他类型的rspec-rails规范不同,功能规范是通过featurescenario方法定义的.

Unlike other types of rspec-rails spec, feature specs are defined with the feature and scenario methods.

功能规格(仅功能规格)使用Capybara的所有功能,包括visitfill_inclick_button之类的方法以及have_text之类的匹配器.

Feature specs, and only feature specs, use all of Capybara's functionality, including visit, methods like fill_in and click_button, and matchers like have_text.

有关功能规格的rspec-rails文档.这是一个快速的例子:

There are plenty of examples in the rspec-rails documentation for feature specs. Here's a quick one:

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 spec just renders a view in isolation, with template variables provided by the test rather than by controllers.

与其他类型的rspec-rails规范一样,视图规范是用describeit方法定义的.一个使用assign分配模板变量,使用render渲染视图,并使用rendered获取结果.

Like other types of rspec-rails spec, view specs are defined with the describe and it methods. One assigns template variables with assign, renders the view with render and gets the results with rendered.

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

The only Capybara functionality used in view specs is the matchers, like have_text.

视图规格的rspec-rails文档.这是一个快速的例子:

There are plenty of examples in the rspec-rails documentation of view specs. Here's a quick one:

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

这篇关于功能规格和视图规格之间有什么区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 23:22