使用辅助方法之前挂钩的范围问题?

发布于 2024-12-18 08:15:05 字数 3736 浏览 0 评论 0原文

我相信我在范围界定方面遇到了一些问题。

我有一个控制器规格文件,用于测试品牌资源。 在文件的开头,我测试了不同资源的访问权限 上下文块中的用户 a) 未登录 b) 非管理员用户登录 - 我调用我自己的帮助方法,login_user c) 管理员用户登录 - 我调用我自己的帮助方法, 登录管理员用户 那里的规格确实成功通过了。

然后,我创建另一个上下文块来测试资源 已登录的管理员用户。 我尝试按照前面的方法在 before 挂钩中调用 login_admin_user 规格。

它失败了,我怀疑当前范围在 before 钩子内 没有看到我的自定义帮助程序方法,login_admin_user。 这是错误消息:

--------- 提取开始 -----------------

/usr/local/rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.9/lib/
active_support/dependencies.rb:235:in `load': /Users/anexiole/projects/
try_rails/spec/controllers/brands_controller_spec.rb:164: syntax
error, unexpected keyword_end, expecting $end (SyntaxError)
  from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.9/lib/
active_support/dependencies.rb:235:in `block in load'
  from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.9/lib/
active_support/dependencies.rb:227:in `load_dependency'
  from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.9/lib/
active_support/dependencies.rb:235:in `load'
  from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/
rspec/core/configuration.rb:419:in `block in load_spec_files'
  from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/
rspec/core/configuration.rb:419:in `map'
  from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/
rspec/core/configuration.rb:419:in `load_spec_files'
  from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/
rspec/core/command_line.rb:18:in `run'
  from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/
rspec/core/runner.rb:80:in `run_in_process'
  from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/
rspec/core/runner.rb:69:in `run'
  from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/
rspec/core/runner.rb:11:in `block in autorun'

---------- 提取结束 ---- --------------

我的规格如下:

---------spec/controllers/brands_controller_spec.rb (start)

require 'spec_helper'

describe BrandsController do

  # This should return the minimal set of attributes required to create a valid
  # Brand. As you add validations to Brand, be sure to
  # update the return value of this method accordingly.
  def valid_attributes
    {
        'name' => 'J Speed',
        'description' => 'From gunsai province'
    }
  end


    context 'checking access for varying users' do
        describe 'brands access is not available to users who have not
signed in' do
            it 'users that are not logged in will be sent to the sign
in page' do
                get :index
                response.should redirect_to(new_user_session_path)
            end
        end

        describe 'brands access is not available to regular users' do
            login_user

            it 'regular users that are logged in will be sent to home
page' do
                get :index
                response.should redirect_to(root_path)
            end
        end

        describe 'brands access is available only to admin users' do
            login_admin_user

            it 'admin users that are logged in can access the index
page' do
                get :index
                response.should render_template('index')
            end
        end
    end

    context 'with an admin user signed in' do    # <----- starts
failing in this context
        before(:each) do
            login_admin_user
        end

        describe "creates a new brand entry" do
            it "assigns a new brand as @brand" do
                get :new
                assigns(:brand).should be_a_new(Brand)
            end
        end
    end
end

---------spec/控制器/brands_controller_spec.rb(结束)

I believe I have some problem with scoping.

I have a controller spec file which tests a brands resource.
At the start of the file, I test the resource's access for different
users in a context block
a) not signed in
b) non-admin user signed in- I call my own helper method, login_user
c) admin user signed in - I call my own helper method,
login_admin_user
Specs there do pass successfully.

I then create another context block to just test the resource for an
admin user that's signed in.
I tried calling login_admin_user in a before hook as per the previous
specs .

It fails and I suspect that the current scope within the before hook
does not see my custom helper method, login_admin_user.
Here is the error message:

--------- Extract start -----------------

/usr/local/rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.9/lib/
active_support/dependencies.rb:235:in `load': /Users/anexiole/projects/
try_rails/spec/controllers/brands_controller_spec.rb:164: syntax
error, unexpected keyword_end, expecting $end (SyntaxError)
  from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.9/lib/
active_support/dependencies.rb:235:in `block in load'
  from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.9/lib/
active_support/dependencies.rb:227:in `load_dependency'
  from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.0.9/lib/
active_support/dependencies.rb:235:in `load'
  from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/
rspec/core/configuration.rb:419:in `block in load_spec_files'
  from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/
rspec/core/configuration.rb:419:in `map'
  from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/
rspec/core/configuration.rb:419:in `load_spec_files'
  from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/
rspec/core/command_line.rb:18:in `run'
  from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/
rspec/core/runner.rb:80:in `run_in_process'
  from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/
rspec/core/runner.rb:69:in `run'
  from /usr/local/rvm/gems/ruby-1.9.2-p290/gems/rspec-core-2.6.4/lib/
rspec/core/runner.rb:11:in `block in autorun'

---------- Extract end ------------------

My specs is as follows:

--------- spec/controllers/brands_controller_spec.rb (start)

require 'spec_helper'

describe BrandsController do

  # This should return the minimal set of attributes required to create a valid
  # Brand. As you add validations to Brand, be sure to
  # update the return value of this method accordingly.
  def valid_attributes
    {
        'name' => 'J Speed',
        'description' => 'From gunsai province'
    }
  end


    context 'checking access for varying users' do
        describe 'brands access is not available to users who have not
signed in' do
            it 'users that are not logged in will be sent to the sign
in page' do
                get :index
                response.should redirect_to(new_user_session_path)
            end
        end

        describe 'brands access is not available to regular users' do
            login_user

            it 'regular users that are logged in will be sent to home
page' do
                get :index
                response.should redirect_to(root_path)
            end
        end

        describe 'brands access is available only to admin users' do
            login_admin_user

            it 'admin users that are logged in can access the index
page' do
                get :index
                response.should render_template('index')
            end
        end
    end

    context 'with an admin user signed in' do    # <----- starts
failing in this context
        before(:each) do
            login_admin_user
        end

        describe "creates a new brand entry" do
            it "assigns a new brand as @brand" do
                get :new
                assigns(:brand).should be_a_new(Brand)
            end
        end
    end
end

--------- spec/controllers/brands_controller_spec.rb (end)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋日私语 2024-12-25 08:15:05

login_admin_user 创建了一个 before 钩子,但这里它位于 before 的内部
钩。那是行不通的。尝试:

context 'with an admin user signed in' do
  login_admin_user # not inside a before hook

  describe "creates a new brand entry" do
    it "assigns a new brand as @brand" do
      get :new
      assigns(:brand).should be_a_new(Brand)
    end
  end
end

login_admin_user creates a before hook, but here it's inside a before
hook. That doesn't work. Try:

context 'with an admin user signed in' do
  login_admin_user # not inside a before hook

  describe "creates a new brand entry" do
    it "assigns a new brand as @brand" do
      get :new
      assigns(:brand).should be_a_new(Brand)
    end
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文