使用 SentientUser 配置 Cucumber

发布于 2024-08-26 18:00:14 字数 4507 浏览 2 评论 0原文

我有一个使用 SentientUser gem 向我的模型提供当前用户的应用程序。最重要的是,我使用默认范围来确保用户只能访问他们拥有的数据。所以对我来说,一个典型的模型看起来像这样:

class Location < ActiveRecord::Base
  validates_presence_of :name, :time_zone, :address

  belongs_to :account
  belongs_to :address

  accepts_nested_attributes_for :address

  default_scope :conditions => { :account_id => User.current.account }
end

当我运行 rake cucumber 时,我在 default_scope 行上收到一个关于帐户为零的错误。我的猜测是,当 Cucumber rake 任务运行时,它无法访问正确的应用程序控制器来注入 current_user 方法,因此 SentientUser 无法获取当前用户?我得到的错误是:

rake cucumber
(in /Users/corithmalin/Documents/Code/Ruby/flavorpulse)
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -I "/Library/Ruby/Gems/1.8/gems/cucumber-0.6.3/lib:lib" "/Library/Ruby/Gems/1.8/gems/cucumber-0.6.3/bin/cucumber"  --profile default
Using the default profile...
undefined method `account' for nil:NilClass (NoMethodError)
/Users/corithmalin/Documents/Code/Ruby/flavorpulse/vendor/rails/activesupport/lib/active_support/whiny_nil.rb:52:in `method_missing'
/Users/corithmalin/Documents/Code/Ruby/flavorpulse/app/models/location.rb:9
/Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
/Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `polyglot_original_require'
/Library/Ruby/Gems/1.8/gems/polyglot-0.3.0/lib/polyglot.rb:65:in `require'
/Users/corithmalin/Documents/Code/Ruby/flavorpulse/vendor/rails/activesupport/lib/active_support/dependencies.rb:158:in `require'
/Users/corithmalin/Documents/Code/Ruby/flavorpulse/vendor/rails/activesupport/lib/active_support/dependencies.rb:265:in `require_or_load'
/Users/corithmalin/Documents/Code/Ruby/flavorpulse/vendor/rails/activesupport/lib/active_support/dependencies.rb:224:in `depend_on'
/Users/corithmalin/Documents/Code/Ruby/flavorpulse/vendor/rails/activesupport/lib/active_support/dependencies.rb:136:in `require_dependency'
/Users/corithmalin/Documents/Code/Ruby/flavorpulse/config/../vendor/rails/railties/lib/initializer.rb:414:in `load_application_classes'
/Users/corithmalin/Documents/Code/Ruby/flavorpulse/config/../vendor/rails/railties/lib/initializer.rb:413:in `each'
/Users/corithmalin/Documents/Code/Ruby/flavorpulse/config/../vendor/rails/railties/lib/initializer.rb:413:in `load_application_classes'
/Users/corithmalin/Documents/Code/Ruby/flavorpulse/config/../vendor/rails/railties/lib/initializer.rb:411:in `each'
/Users/corithmalin/Documents/Code/Ruby/flavorpulse/config/../vendor/rails/railties/lib/initializer.rb:411:in `load_application_classes'
/Users/corithmalin/Documents/Code/Ruby/flavorpulse/config/../vendor/rails/railties/lib/initializer.rb:197:in `process'
/Users/corithmalin/Documents/Code/Ruby/flavorpulse/config/../vendor/rails/railties/lib/initializer.rb:113:in `send'
/Users/corithmalin/Documents/Code/Ruby/flavorpulse/config/../vendor/rails/railties/lib/initializer.rb:113:in `run'
/Users/corithmalin/Documents/Code/Ruby/flavorpulse/config/environment.rb:9
/Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
/Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `polyglot_original_require'
/Library/Ruby/Gems/1.8/gems/polyglot-0.3.0/lib/polyglot.rb:65:in `require'
/Users/corithmalin/Documents/Code/Ruby/flavorpulse/features/support/env.rb:8
/Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
/Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `polyglot_original_require'
/Library/Ruby/Gems/1.8/gems/polyglot-0.3.0/lib/polyglot.rb:65:in `require'
/Library/Ruby/Gems/1.8/gems/cucumber-0.6.3/bin/../lib/cucumber/rb_support/rb_language.rb:124:in `load_code_file'
/Library/Ruby/Gems/1.8/gems/cucumber-0.6.3/bin/../lib/cucumber/step_mother.rb:85:in `load_code_file'
/Library/Ruby/Gems/1.8/gems/cucumber-0.6.3/bin/../lib/cucumber/step_mother.rb:77:in `load_code_files'
/Library/Ruby/Gems/1.8/gems/cucumber-0.6.3/bin/../lib/cucumber/step_mother.rb:76:in `each'
/Library/Ruby/Gems/1.8/gems/cucumber-0.6.3/bin/../lib/cucumber/step_mother.rb:76:in `load_code_files'
/Library/Ruby/Gems/1.8/gems/cucumber-0.6.3/bin/../lib/cucumber/cli/main.rb:48:in `execute!'
/Library/Ruby/Gems/1.8/gems/cucumber-0.6.3/bin/../lib/cucumber/cli/main.rb:20:in `execute'
/Library/Ruby/Gems/1.8/gems/cucumber-0.6.3/bin/cucumber:8
rake aborted!
Command failed with status (1): [/System/Library/Frameworks/Ruby.framework/...]

(See full trace by running task with --trace)

I have an application using the SentientUser gem to provide the current user to my models. On top of that I'm using default scoping to ensure that a user can only ever access data that they own. So a typical model for me looks something like this:

class Location < ActiveRecord::Base
  validates_presence_of :name, :time_zone, :address

  belongs_to :account
  belongs_to :address

  accepts_nested_attributes_for :address

  default_scope :conditions => { :account_id => User.current.account }
end

When I run rake cucumber I get an error on the default_scope line about account being nil. My guess is that when the cucumber rake task is run it doesn't have access to the correct application controller to inject the current_user method and thus SentientUser can't get the current user? The error I get is:

rake cucumber
(in /Users/corithmalin/Documents/Code/Ruby/flavorpulse)
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -I "/Library/Ruby/Gems/1.8/gems/cucumber-0.6.3/lib:lib" "/Library/Ruby/Gems/1.8/gems/cucumber-0.6.3/bin/cucumber"  --profile default
Using the default profile...
undefined method `account' for nil:NilClass (NoMethodError)
/Users/corithmalin/Documents/Code/Ruby/flavorpulse/vendor/rails/activesupport/lib/active_support/whiny_nil.rb:52:in `method_missing'
/Users/corithmalin/Documents/Code/Ruby/flavorpulse/app/models/location.rb:9
/Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
/Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `polyglot_original_require'
/Library/Ruby/Gems/1.8/gems/polyglot-0.3.0/lib/polyglot.rb:65:in `require'
/Users/corithmalin/Documents/Code/Ruby/flavorpulse/vendor/rails/activesupport/lib/active_support/dependencies.rb:158:in `require'
/Users/corithmalin/Documents/Code/Ruby/flavorpulse/vendor/rails/activesupport/lib/active_support/dependencies.rb:265:in `require_or_load'
/Users/corithmalin/Documents/Code/Ruby/flavorpulse/vendor/rails/activesupport/lib/active_support/dependencies.rb:224:in `depend_on'
/Users/corithmalin/Documents/Code/Ruby/flavorpulse/vendor/rails/activesupport/lib/active_support/dependencies.rb:136:in `require_dependency'
/Users/corithmalin/Documents/Code/Ruby/flavorpulse/config/../vendor/rails/railties/lib/initializer.rb:414:in `load_application_classes'
/Users/corithmalin/Documents/Code/Ruby/flavorpulse/config/../vendor/rails/railties/lib/initializer.rb:413:in `each'
/Users/corithmalin/Documents/Code/Ruby/flavorpulse/config/../vendor/rails/railties/lib/initializer.rb:413:in `load_application_classes'
/Users/corithmalin/Documents/Code/Ruby/flavorpulse/config/../vendor/rails/railties/lib/initializer.rb:411:in `each'
/Users/corithmalin/Documents/Code/Ruby/flavorpulse/config/../vendor/rails/railties/lib/initializer.rb:411:in `load_application_classes'
/Users/corithmalin/Documents/Code/Ruby/flavorpulse/config/../vendor/rails/railties/lib/initializer.rb:197:in `process'
/Users/corithmalin/Documents/Code/Ruby/flavorpulse/config/../vendor/rails/railties/lib/initializer.rb:113:in `send'
/Users/corithmalin/Documents/Code/Ruby/flavorpulse/config/../vendor/rails/railties/lib/initializer.rb:113:in `run'
/Users/corithmalin/Documents/Code/Ruby/flavorpulse/config/environment.rb:9
/Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
/Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `polyglot_original_require'
/Library/Ruby/Gems/1.8/gems/polyglot-0.3.0/lib/polyglot.rb:65:in `require'
/Users/corithmalin/Documents/Code/Ruby/flavorpulse/features/support/env.rb:8
/Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
/Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `polyglot_original_require'
/Library/Ruby/Gems/1.8/gems/polyglot-0.3.0/lib/polyglot.rb:65:in `require'
/Library/Ruby/Gems/1.8/gems/cucumber-0.6.3/bin/../lib/cucumber/rb_support/rb_language.rb:124:in `load_code_file'
/Library/Ruby/Gems/1.8/gems/cucumber-0.6.3/bin/../lib/cucumber/step_mother.rb:85:in `load_code_file'
/Library/Ruby/Gems/1.8/gems/cucumber-0.6.3/bin/../lib/cucumber/step_mother.rb:77:in `load_code_files'
/Library/Ruby/Gems/1.8/gems/cucumber-0.6.3/bin/../lib/cucumber/step_mother.rb:76:in `each'
/Library/Ruby/Gems/1.8/gems/cucumber-0.6.3/bin/../lib/cucumber/step_mother.rb:76:in `load_code_files'
/Library/Ruby/Gems/1.8/gems/cucumber-0.6.3/bin/../lib/cucumber/cli/main.rb:48:in `execute!'
/Library/Ruby/Gems/1.8/gems/cucumber-0.6.3/bin/../lib/cucumber/cli/main.rb:20:in `execute'
/Library/Ruby/Gems/1.8/gems/cucumber-0.6.3/bin/cucumber:8
rake aborted!
Command failed with status (1): [/System/Library/Frameworks/Ruby.framework/...]

(See full trace by running task with --trace)

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文