Is there any difference between a feature spec and a view spec?

允我心安 提交于 2019-12-07 10:47:11

问题


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

Feature tests should be for testing larger behaviours in the system.

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.


回答1:


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

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.

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

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.

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.

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.

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

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


来源:https://stackoverflow.com/questions/35258592/is-there-any-difference-between-a-feature-spec-and-a-view-spec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!