Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nesting scenarios with feature specs in Rspec Rails 3

I'm using Rspec Rails with Capybara for testing and I want to use the new feature spec in RSpec Rails 3 as they read more as customer tests and acceptance tests. However, one thing I find missing from the older (Describe/It) style is nesting. When trying to nest scenarios or use background inside any scenario block, I get an undefined method error. Is there anyway I could achieve nesting with feature specs to get something like this (from Michael Hartl's Ruby On Rails Tutorial:

describe "Authentication" do
    subject { page }

    describe "authorization" do
        let(:user) { FactoryGirl.create(:user) }

        describe "for non-signed in users" do

            describe "when attempting to visit a protected page" do
                before { visit edit_user_path(user) }
                it "should redirect_to to the signin page" do
                    expect(page).to have_title('Sign in')
                end

            describe "after signing in" do
                before do
                    valid_signin user, no_visit: true
                end

                it "should render the desired protected page" do
                    expect(page).to have_title('Edit user')
                end

Or should I be thinking in a different way about integration tests ?

like image 244
Karim Sonbol Avatar asked Mar 17 '14 22:03

Karim Sonbol


2 Answers

As described in https://www.relishapp.com/rspec/rspec-rails/docs/feature-specs/feature-spec, feature corresponds to describe and scenario corresponds to it. So, you can nest instances of feature, but you cannot nest a scenario within a scenario, just as you cannot nest an it within a it.

like image 121
Peter Alfvin Avatar answered Oct 21 '22 02:10

Peter Alfvin


Nested feature with scenarios is available in Capybara version 2.2.1

In Gemfile include

gem "capybara", "~> 2.2.1"

and bundle install

As per official documentation of Capybara

feature is in fact just an alias for describe ..., :type => :feature, background is an alias for before, scenario for it, and given/*given!* aliases for let/*let!*, respectively.

Here is the original issue and later it was accepted and merged in version 2.2.1

like image 38
Kirti Thorat Avatar answered Oct 21 '22 04:10

Kirti Thorat