2

У пользователей есть доступ к разделам сайта / функциям. Например есть admin - который может все и guest - у которого есть доступ только к просмотру информации.

Сейчас например тест выглядит примерно так:

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

Вопрос, правильный ли это подход, тестировать разные виды ролей в спеке про комментарии? Или же вынести эти тесты вообще в отдельный раздел, например:

spec/user/authorization/comment_spec.rb

Или может это все вообще в корне не верно и есть устоявшийся паттерн для таких случаев?

1 ответ 1

0

Обычно я тестирую такой функционал в фуннциональных тестах

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

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.