使用 Cucumber 从 lib 中获取身份验证文件

发布于 2024-08-24 10:30:04 字数 2148 浏览 5 评论 0原文

我第一次尝试 Cucumber 时遇到了一个问题。我尝试使用 RyanB 的漂亮身份验证生成器,但似乎我无法使用帮助程序运行我的 Cucumber 功能时的方法。

我的部分功能给我带来了问题:

when I am on the new book page                                   # features/step_definitions/web_steps.rb:18
      undefined local variable or method `current_user' for #<Cucumber::Rails::World:0x13a3b2a> (NameError)
      ./features/support/paths.rb:15:in `path_to'
      ./features/step_definitions/web_steps.rb:19:in `/^(?:|I )am on (.+)$/'
      features/user_story.feature:30:in `When I am on the new book page'

support/paths.rb:

when /the new book page/
  user_path(current_user)+'/books/new'

routes.rb:

resources :users do
  resources :books
end

为我提供身份验证帮助器方法(如 current_user)的代码位于我的 lib/ 文件夹内。我尝试过:

require "#{Rails.root}/lib/authentication"

在我的 env.rb 中,但这似乎没有任何作用。那么我应该在这里做什么呢?我觉得应该有一个简单的解决方案,但这只是我的想法。

额外信息

gem env :

RubyGems Environment:
  - RUBYGEMS VERSION: 1.3.6
  - RUBY VERSION: 1.9.2 (2010-03-04 patchlevel -1) [i386-darwin9.8.0]
  - INSTALLATION DIRECTORY: /Users/ekoslow/.rvm/gems/ruby-1.9.2-head%rails3beta
  - RUBY EXECUTABLE: /Users/ekoslow/.rvm/rubies/ruby-1.9.2-head/bin/ruby
  - EXECUTABLE DIRECTORY: /Users/ekoslow/.rvm/gems/ruby-1.9.2-head%rails3beta/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86-darwin-9
  - GEM PATHS:
     - /Users/ekoslow/.rvm/gems/ruby-1.9.2-head%rails3beta
     - /Users/ekoslow/.rvm/gems/ruby-1.9.2-head%global
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
     - "gem" => "--no-ri --no-rdoc"
     - :sources => ["http://gems.rubyforge.org", "http://gems.github.com"]
  - REMOTE SOURCES:
     - http://gems.rubyforge.org
     - http://gems.github.com

我正在运行 Rails 3.0.0.beta,但我认为这与本例中的任何内容无关。

I am trying out Cucumber for the first time and I've come accross an issue. I am try to use RyanB's nifty authentication generator, but it seems I can't use the helper methods when running my Cucumber features.

Part of my feature giving me problems:

when I am on the new book page                                   # features/step_definitions/web_steps.rb:18
      undefined local variable or method `current_user' for #<Cucumber::Rails::World:0x13a3b2a> (NameError)
      ./features/support/paths.rb:15:in `path_to'
      ./features/step_definitions/web_steps.rb:19:in `/^(?:|I )am on (.+)$/'
      features/user_story.feature:30:in `When I am on the new book page'

support/paths.rb :

when /the new book page/
  user_path(current_user)+'/books/new'

routes.rb :

resources :users do
  resources :books
end

The piece of code that is giving me my authentication helper methods (like current_user) is inside my lib/ folder. I've tried:

require "#{Rails.root}/lib/authentication"

In my env.rb, but that doesn't seem to do anything. So what should I be doing here instead? I feel there should be a simple solution to this, but it's just going over my head.

Extra Info

gem env :

RubyGems Environment:
  - RUBYGEMS VERSION: 1.3.6
  - RUBY VERSION: 1.9.2 (2010-03-04 patchlevel -1) [i386-darwin9.8.0]
  - INSTALLATION DIRECTORY: /Users/ekoslow/.rvm/gems/ruby-1.9.2-head%rails3beta
  - RUBY EXECUTABLE: /Users/ekoslow/.rvm/rubies/ruby-1.9.2-head/bin/ruby
  - EXECUTABLE DIRECTORY: /Users/ekoslow/.rvm/gems/ruby-1.9.2-head%rails3beta/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86-darwin-9
  - GEM PATHS:
     - /Users/ekoslow/.rvm/gems/ruby-1.9.2-head%rails3beta
     - /Users/ekoslow/.rvm/gems/ruby-1.9.2-head%global
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
     - "gem" => "--no-ri --no-rdoc"
     - :sources => ["http://gems.rubyforge.org", "http://gems.github.com"]
  - REMOTE SOURCES:
     - http://gems.rubyforge.org
     - http://gems.github.com

And I'm running Rails 3.0.0.beta, but I don't think that has to do with anything in this case.

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

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

发布评论

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

评论(3

幻梦 2024-08-31 10:30:04

大概您的应用程序要求您登录才能查看新的图书页面。

如果是这种情况,那么“正在登录”是此测试的先决条件,因此应该在您的Givens中。

因此,使用“假设我已登录”开始测试并使用“我已登录”代码来定义 current_user

Presumably your application requires you to be logged in to view the new book page.

If that is the case then "Being logged" in is a pre-requisite for this test so should be in your Givens.

So start the test with Given I am logged in and use the I am logged in code to define current_user

热情消退 2024-08-31 10:30:04

current_user 仅在您的控制器上下文中定义。在你的黄瓜规范中,你只有外部数据。

一个好的黄瓜测试点击按钮,而不是直接进入 URL。因此,您确实需要知道这个 URL 是什么,或者您可以点击进入哪里。

current_user is define only in your Controller context. In your cucumber spec you have only externe data.

A good cucumber test click on button, not go directly on URL. So you do need know how is this URL or where you can click to go to.

ぃ弥猫深巷。 2024-08-31 10:30:04

不要创建实例变量

使用类似 http://www.francisfish.com/debugging_cucumber_scripts_cucumber_and_devise_authenticati 的内容登录。 htm

(向下滚动到有关 devise 插件的部分)

此示例代码适用于 devise gem,但您所需要做的就是填写登录页面上的正确字段。

这使您可以使用 GUI 并成为应用程序的客户端。

Don't create an instance variable

Log in using something like http://www.francisfish.com/debugging_cucumber_scripts_cucumber_and_devise_authenticati.htm

(scroll down to the bit about the devise plugin)

This example code is for the devise gem but all you need to do is fill in the right fields on the login page.

This makes you exercise the GUI and be a client to the app.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文