2017-03-29 1 views
0

내 응용 프로그램에는 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 

모든 것이 작동합니다!

답변

1

여기에 여러 가지 문제가 있습니다.

첫 번째로 연결된 다른 질문은 카피 바라를 사용하지 않으므로 matchers에 대한 예제를 복사하는 것이 잘못되었습니다.

두 번째로보기 테스트 또는 기능 테스트가 아닌 컨트롤러 테스트를 작성합니다. 컨트롤러 테스트는 기본적으로 페이지를 렌더링하지 않으므로 작성하려는 페이지의 요소를 테스트 테스트 또는 기능 테스트로 테스트합니다. Capybara는 기능 테스트를 위해 설계되었으며 컨트롤러 테스트를 위해 설계되지 않았습니다. 따라서 기본 capybara/rspec 구성 파일에는 'feature'유형의 테스트에 Capybara DSL 만 포함됩니다. 또한 Capybara RSpec matchers는 뷰 테스트에 포함되어 있는데 렌더링 된 문자열이 유용하기 때문에 뷰 테스트에 포함됩니다.

세 번째로 동일한 파일에 get/responsevisit/page을 혼용합니다. 당신은 기능 테스트 등이 다시 작성하는 경우

것은, 당신은 당신이 링크를하지 않습니다 있는지 확인하려면 당신이

expect(page).not_to have_link(href: '...') 

할 것 카피 바라의 특정 HREF와 링크가 없습니다 확인 주어진 텍스트와 주어진 HREF 모두 링크가없는 검사 여전히 텍스트 또는 href를

+0

감사와 링크가있을 수 : 특정 텍스트 및 특정 HREF

expect(page).not_to have_link('link text', href: '...') 

참고로 존재 너!spec/features에 모든 것을 집어 넣어 실제로 문제를 해결했습니다. 적어도 get/response와'visit/page'을 섞어 적어도 하나의 테스트 케이스를 처리하려고 시도했습니다. – mohnstrudel