1

我正在尝试使以下工作:

#spec/features/senseis_spec.rb
require 'spec_helper'

describe "Senseis" do
  subject { page }
  @paths = [senseis_path, new_sensei_path, edit_sensei_path(1), sensei_path(1)]

  describe "sensei can not access" do
    before { login_as_sensei }

    @paths.each do |path|
      describe "#{path}" do
        before { visit path }
        it { should have_selector(error, text: "You are not authorized") }
      end
    end
  end

end

但这给了我一个错误NameError: undefined local variable or method 'senseis_path'

我尝试添加config.include Rails.application.routes.url_helpers到规范助手,但没有帮助。

很长的路要走:

describe "sensei can not access" do
  before { login_as_sensei }

  describe "senseis#index" do
    before { visit senseis_path }
    it { should have_selector(error, text: "You are not authorized") }
  end
end

理想情况下,我想在另一个文件中定义它,然后调用deny_access_to(path_one, path_two, ...)

PSerror在别处定义。

路线:

                  root        /                             dashboard#index
               senseis GET    /senseis(.:format)            senseis#index
                       POST   /senseis(.:format)            senseis#create
            new_sensei GET    /senseis/new(.:format)        senseis#new
           edit_sensei GET    /senseis/:id/edit(.:format)   senseis#edit
                sensei GET    /senseis/:id(.:format)        senseis#show
                       PUT    /senseis/:id(.:format)        senseis#update
                       DELETE /senseis/:id(.:format)        senseis#destroy
    new_sensei_session GET    /user/sign_in(.:format)       devise/sessions#new
        sensei_session POST   /user/sign_in(.:format)       devise/sessions#create
destroy_sensei_session DELETE /user/sign_out(.:format)      devise/sessions#destroy
       sensei_password POST   /user/password(.:format)      devise/passwords#create
   new_sensei_password GET    /user/password/new(.:format)  devise/passwords#new
  edit_sensei_password GET    /user/password/edit(.:format) devise/passwords#edit
                       PUT    /user/password(.:format)      devise/passwords#update
4

1 回答 1

2

尝试类似的东西

require 'spec_helper'

describe "Senseis" do

  def should_deny_access_to(paths)
    paths.each do |path|
      login_as_sensei
      visit path
      page.should have_selector(error, text: "You are not authorized")
    end
  end

  it { should_deny_access_to [senseis_path, new_sensei_path, edit_sensei_path(1), sensei_path(1)] }

end 

或使用共享示例组。

于 2013-02-16T12:41:03.190 回答