Users have access to sections of the site / functions. For example, there is admin - which can do everything and guest - which has access only to view information.

Now for example, the test looks like this:

feature 'Comments' do context 'User' do scenario 'can add comments to question' scenario 'can add comments to posts' scenario "can't add replies to own comment" context 'Guest' do scenario "can't add comments to question" scenario "can't add comments to posts" end end 

The question is, is this the right approach, to test different types of roles in the spec about comments? Or, to put these tests in general in a separate section, for example:

spec / user / authorization / comment_spec.rb

Or maybe all this is fundamentally wrong and there is an established pattern for such cases?

    1 answer 1

    I usually test this functionality in functional tests.

     RSpec.describe SpecialitiesController, type: :controller do subject { response } let!(:user) { create(:user, :admin) } let!(:speciality) { create(:speciality) } before do allow_any_instance_of(described_class).to receive(:current_user).and_return(user) end describe 'GET #new' do before { get :new } it { is_expected.to have_http_status 200 } it { is_expected.to render_template 'new' } context 'when user hasn\'t admin privileges' do let!(:user) { create(:user) } before { get :new } it { expect(flash[:alert]).to eq 'You can\'t access this page without admin privileges' } it { is_expected.to redirect_to root_path } end end end