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

时间:2011-11-28 06:20:05

标签: rspec

我相信我在确定范围方面存在一些问题。

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

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

它失败了,我怀疑前钩内的当前范围 没有看到我的自定义帮助方法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 / controllers / brands_controller_spec.rb(结束)

1 个答案:

答案 0 :(得分:1)

login_admin_user创建了一个前钩子,但是之前它是里面 a 钩。这不起作用。尝试:

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