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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With