私のアプリケーションには、スーパー管理者(プロパティー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から取られている「ブロック期待」)、それらのどれを作業。私はここで
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される使用:アップデートはトーマスの答えを使用して
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
私はspec/features
に私のコードを入れてしまった、それはこの権利のようになります。現在:
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
すべて機能します。
感謝とのリンクがあるかもしれませんあなた!スペック/フィーチャーにすべてを入れてみると、本当に問題は解決しました。私は 'get/response'と' visit/page'を混ぜ合わせて、少なくとも1つのテストケースを処理しようとしました。 – mohnstrudel