내 응용 프로그램에는 superadmin (사용자가 superadmin: true
인 사용자)으로 제한되는 관리 부분이 있습니다. 나는 쇼핑 목록을 가지고 있는데, 나는 페이지를 매기고 테스트하기를 원한다.테스트는 Rspec/Capybara로 보석에 페이지를 매 깁니다.
save_and_open_page
으로 현재 코드를 디버깅 할 때 빈 페이지가 표시됩니다. superadmin으로 로그인하지 않으면 응용 프로그램의 루트로 리디렉션되고 save_and_open_page
으로 디버깅하려고하면 루트 페이지가 표시됩니다 .. 전혀 로그인하지 않으면 로그인 페이지로 리디렉션됩니다. 따라서 기본 기능이 작동해야합니다.
내가 superadmin
과 작동하지 않는 이유가 무엇인지, save_and_open_page
으로 디버깅 할 때 상점 목록이 표시되지 않는 이유가 무엇인지 알지 못합니다.
이 내 spec/controllers/shops_controller_spec.rb
(here에서 기본적으로 복사) :
require 'rails_helper'
RSpec.describe Admin::ShopsController, type: :controller do
context "GET methods" do
describe "#index action" do
before(:all) {
amount = Rails.application.config.page_size
amount.times { FactoryGirl.create(:shop) }
}
before(:each) {
login_as(FactoryGirl.create(:user, superadmin: true), :scope => :user)
}
context "with entries == config.page_size" do
it "has no second page" do
get :index
expect(response).not_to have_selector("a", :href => "/shops?page=2", :content => "2")
# visit admin_shops_path
# expect(page).to have_no_xpath("//*[@class='pagination']//a[text()='2']")
end
end
context "with entries > config.page_size" do
before { FactoryGirl.create(:shop) }
it "has a second page with too many entries" do
visit "/admin/shops"
save_and_open_page
expect(page).to have_xpath("//*[@class='pagination']//a[text()='2']")
end
it "correctly redirects to next page" do
visit admin_shops_path
find("//*[@class='pagination']//a[text()='2']").click
expect(page.status_code).to eq(200)
end
end
end
end
end
당신이 볼 수 있듯이, 나는 다른 방법으로 테스트하려고합니다 (this SO-question에서 가져옵니다 "블록 기대"),하지만 그들 중 누구도를 작업. 내가 spec/features
에 내 코드를 넣어 결국 토마스의 대답을 사용
class AdminController < ApplicationController
layout 'admin'
before_action :authenticate_user!, :verify_is_superadmin
before_action :set_locale
before_action :get_breadcrumbs
private
def get_breadcrumbs
splitted_url = request.original_fullpath.split("/")
# Remove first object
splitted_url.shift
result = splitted_url.map { |element| element.humanize.capitalize }
session[:breadcrumbs] = result
# debug
end
def set_locale
I18n.locale = params[:locale] || session[:locale] || I18n.default_locale
# session[:locale] = I18n.locale
end
def verify_is_superadmin
(current_user.nil?) ? redirect_to(root_path) : (redirect_to(root_path) unless current_user.superadmin?)
end
end
업데이트 을하고이 권리 같습니다 도움이된다면 내가 여기
Admin::ShopsController GET methods #index action with entries == config.page_size has no second page
Failure/Error: expect(page).not_to have_selector("a", :href => "/shops?page=2", :content => "2")
ArgumentError:
invalid keys :href, :content, should be one of :count, :minimum, :maximum, :between, :text, :id, :class, :visible, :exact, :exact_text, :match, :wait, :filter_set
를받을
get :index
사용하여 내 AdminController.rb입니다 지금 :
require "rails_helper"
RSpec.feature "Widget management", :type => :feature do
before(:each) {
amount = Rails.application.config.page_size
amount.times { FactoryGirl.create(:shop) }
}
before(:each) {
login_as(FactoryGirl.create(:user, superadmin: true), :scope => :user)
}
scenario "with entries == config.page_size" do
visit admin_shops_path
#save_and_open_page
expect(page).to have_no_xpath("//*[@class='pagination']//a[text()='2']")
end
scenario "with entries > config.page_size" do
FactoryGirl.create(:shop)
visit admin_shops_path
expect(page).to have_xpath("//*[@class='pagination']//a[text()='2']")
end
scenario "with entries > config.page_size it correctly redirects to next page" do
FactoryGirl.create(:shop)
visit admin_shops_path
find("//*[@class='pagination']//a[text()='2']").click
expect(page.status_code).to eq(200)
end
end
모든 것이 작동합니다!
감사와 링크가있을 수 : 특정 텍스트 및 특정 HREF
참고로 존재 너!spec/features에 모든 것을 집어 넣어 실제로 문제를 해결했습니다. 적어도 get/response와'visit/page'을 섞어 적어도 하나의 테스트 케이스를 처리하려고 시도했습니다. – mohnstrudel