如何让故事与 Restful_authentication 和 Cucumber 一起使用?

发布于 2024-07-16 14:04:31 字数 1919 浏览 5 评论 0原文

克隆最新稳定版本的

到一个干净的 Rails 应用程序中,以及遵循(我相信的)每个插件的所有说明,黄瓜故事仍然失败:-(。 以下是问题的总结:

  1. 尽管创建了 'map.root :controller => ,但重定向并没有立即起作用。 “my_controller”路线:
    预期重定向到“/”,但没有重定向(Spec::Expectations::ExpectationNotMetError) 
      /cygdrive/c/development/test/vendor/plugins/rspec/lib/spec/expectations.rb:57:in `fail_with' 
      /cygdrive/c/development/test/vendor/plugins/rspec/lib/spec/expectations/handler.rb:14:在`handle_matcher'中 
      /cygdrive/c/development/test/vendor/plugins/rspec/lib/spec/expectations/extensions/object.rb:31:在“应该”中。 
      /features/step_definitions/user_steps.rb:111:在`/^an?   (.*) 用户名​​为“(.*)”$/' 
      features/sessions.feature:25:in `以及一个名为“reggie”的激活用户” 
      
  2. 该报道称,尽管 features/step_definitions/ra_env.rb 文件调用,但 logged_in? 方法仍受到保护:
    ApplicationController.send(:public, :logged\_in?, :current\_user, :authorized?)
    该调用是否无需存根即可使用这些方法?

哦,我正在尝试运行 autospec,所以我执行了以下命令来启动它:

export AUTOFEATURE=true
rake spec:server:start
ruby script/autospec

After cloning the latest stable versions of

into a clean rails application, and following (what I believe are) all the instructions for each plugin, cucumber stories still are failing :-(. Here's a summary of the problems:

  1. redirects are not working right off the bat despite having created the 'map.root :controller => "my_controller"' route :
    expected redirect to "/", got no redirect (Spec::Expectations::ExpectationNotMetError)
    /cygdrive/c/development/test/vendor/plugins/rspec/lib/spec/expectations.rb:57:in `fail_with'
    /cygdrive/c/development/test/vendor/plugins/rspec/lib/spec/expectations/handler.rb:14:in `handle_matcher'
    /cygdrive/c/development/test/vendor/plugins/rspec/lib/spec/expectations/extensions/object.rb:31:in `should'.
    /features/step_definitions/user_steps.rb:111:in `/^an? (.*) user named '(.*)'$/'
    features/sessions.feature:25:in `And an activated user named 'reggie''
    
  2. the story says the logged_in? method is protected despite the features/step_definitions/ra_env.rb file calling:
    ApplicationController.send(:public, :logged\_in?, :current\_user, :authorized?)

    Doesn't that call make those methods available without needing stubbing?

Oh, and I'm trying to run autospec, so I've done the following commands to get it started:

export AUTOFEATURE=true
rake spec:server:start
ruby script/autospec

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

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

发布评论

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

评论(7

瞎闹 2024-07-23 14:04:31

我做了一些研究,这就是我得到的。
ra_response_steps.rb 期望重定向是粗略的,然后用故事来定义是否应该遵循重定向。
这是失败的,因为 Webrat Session 实现具有以下代码:

    def request_page(url, http_method, data) #:nodoc:
      h = headers
      h['HTTP_REFERER'] = @current_url if @current_url

      debug_log "REQUESTING PAGE: #{http_method.to_s.upcase} #{url} with #{data.inspect} and HTTP headers #{h.inspect}"
      if h.empty?
        send "#{http_method}", url, data || {}
      else
        send "#{http_method}", url, data || {}, h
      end

      save_and_open_page if exception_caught? && Webrat.configuration.open_error_files?
      raise PageLoadError.new("Page load was not successful (Code: #{response_code.inspect}):\n#{formatted_error}") unless success_code?

      reset

      @current_url  = url
      @http_method  = http_method
      @data         = data

      if internal_redirect?
        check_for_infinite_redirects
        request_page(response_location, :get, {})
      end

      return response
    end

注意 ifinternal_redirect? ...结束。 这是导致我们测试失败的原因,因为 webrat 正在遵循重定向。
作为解决方法,您可以在 webrat 会话中评论这些行,但这可能不是一个不错的解决方案。 我会多做一点工作并在某个地方发布补丁。

I've done some research and here is what I got.
The ra_response_steps.rb expect redirect to come crude and then the story to define wether the redirect should be followed or not.
This is failing because Webrat Session implementation has the following code:

    def request_page(url, http_method, data) #:nodoc:
      h = headers
      h['HTTP_REFERER'] = @current_url if @current_url

      debug_log "REQUESTING PAGE: #{http_method.to_s.upcase} #{url} with #{data.inspect} and HTTP headers #{h.inspect}"
      if h.empty?
        send "#{http_method}", url, data || {}
      else
        send "#{http_method}", url, data || {}, h
      end

      save_and_open_page if exception_caught? && Webrat.configuration.open_error_files?
      raise PageLoadError.new("Page load was not successful (Code: #{response_code.inspect}):\n#{formatted_error}") unless success_code?

      reset

      @current_url  = url
      @http_method  = http_method
      @data         = data

      if internal_redirect?
        check_for_infinite_redirects
        request_page(response_location, :get, {})
      end

      return response
    end

Notice the if internal_redirect? ... end. This if is the one making our tests failing because webrat is following the redirects.
As a workaround, you can comment those lines on your webrat session but this is probably not a decent solution. I'll work a bit more and post a patch somewhere.

傲娇萝莉攻 2024-07-23 14:04:31

我发现这篇博文很好地解释了问题的症结:

http://blog.andrew.premdas.org/articles/2008/10/15/webrat-visits-and-redirects

基本上,宁静的身份验证测试正在尝试测试不应该的东西使用 webrat 进行测试。 因此,上面建议的更改与我认为可能应该更改的内容相反。

我更改了 Restful 身份验证测试,以便它们不测试重定向,而只测试您最终进入的页面。 但是,某些我不明白的重定向似乎仍然存在问题:

Then she should be at the new session page                     # features/step_definitions/ra_response_steps.rb:15
  expected "session/new", got redirected to "http://www.example.com/session/new" (Spec::Expectations::ExpectationNotMetError)

我不明白这个 example.com 来自哪里。 还有其他人有类似的错误吗?

I found this blog post that explains the crux of the issue well:

http://blog.andrew.premdas.org/articles/2008/10/15/webrat-visits-and-redirects

basically, restful authentication tests are trying to test something that shouldn't be tested using webrat. So, the change recommended above is the opposite of what I think probably should be changed.

I've changed the Restful Authentication Tests so that they don't test redirects but just test what page you end up on. However, there still seems to be a problem with certain redirects that I don't understand:

Then she should be at the new session page                     # features/step_definitions/ra_response_steps.rb:15
  expected "session/new", got redirected to "http://www.example.com/session/new" (Spec::Expectations::ExpectationNotMetError)

I don't understand where this example.com is coming from. anybody else have a similar error?

森林散布 2024-07-23 14:04:31

我必须将 user_steps.rb 中注销函数的定义更改为:

def log_out
  获取“/logout”
end

在尝试获取“/session/destroy”之前,该“/session/destroy”仅在您不删除默认路由时才存在。

另外,请确保您在 application_controller 中“包含 AuthenticatedSystem”。

但仍在解决其他一些问题......

I had to change the definition of the logout function in user_steps.rb to:

def log_out
  get '/logout'
end

Before it was trying to get '/session/destroy' which only exists if you don't remove the default routes.

Also, make sure you "include AuthenticatedSystem" in application_controller.

Still fighting through some of the other issues though...

世界如花海般美丽 2024-07-23 14:04:31

至于“受保护的方法”问题,我发现如果我不使用 autospec 并保留 config.cache_classes=true,那么测试就会通过。

更改 config.cache_classes=false 会再次引入错误。

看来问题要么在于 Rails 中如何实现类缓存,要么在于 rspec 如何管理已创建的类。 不幸的是,我没有资源来进一步调查整个日志,并且似乎对此进行了很好的讨论:
http://群组。 google.com/group/rspec/browse_thread/thread/500ede090bd08996/25a3d9a7d283696b?lnk=gst&q=cache_classes#25a3d9a7d283696b

As for the "Protected method" problem, I figured out that if I don't use autospec and leave config.cache_classes=true, then the tests pass.

Turning config.cache_classes=false introduces the error again.

It appears that the the problem is either with how class caching is implemented in rails, or how rspec manages classes that have been created. Unfortunately I don't have the resources to investigate this a whole log more, and it appears there is a good discussion about it taking place at:
http://groups.google.com/group/rspec/browse_thread/thread/500ede090bd08996/25a3d9a7d283696b?lnk=gst&q=cache_classes#25a3d9a7d283696b

谁许谁一生繁华 2024-07-23 14:04:31

我没有提供太多指导,只是表示同情 - 我最近花了几个小时处理同样的问题。 从好的方面来说,这就是我学习 RSpec 的方式。

我确实发现的一件事是,很多失败都是我想要改变的事情 - 例如,我不想在登录时重定向到“/”,而是重定向到其他地方。

最后,一旦我弄清楚在哪里寻找,大多数故障都很容易修复。

I don't have a lot of guidance to offer, just sympathy - I've spent a few hours recently dealing with the same problem. On the up side, it's how I learned RSpec.

One thing I did find is that a lot of the failures were things I wanted to change anyway - e.g., I didn't want to redirect to '/' on login, but someplace else.

In the end, most of the failures were simple to fix, once I'd figured out where to look.

遮云壑 2024-07-23 14:04:31

我正在解决同样的问题。 我还没到那里,但我认为 ApplicationController.send(:public, :logged_in?, :current_user, :authorized?) 需要改为 support/env.rb 。

I am working through the same issues. I am not there yet, but I think that ApplicationController.send(:public, :logged_in?, :current_user, :authorized?) needs to go in support/env.rb instead.

谁的年少不轻狂 2024-07-23 14:04:31

我也遇到了一些提到的错误。 但我的应用程序中出现的第一个问题是:

多个步骤定义具有相同的正则表达式:

features/step_definitions/user_steps.rb:16:in /^(.*) (.*) usernamed '(.*) '$/'
features/step_definitions/user_steps.rb:29:in
/^没有名为 '(.*) 的用户。
*)'$/'

(Cucumber::Redundant)

当然,我可以解决这个问题,但还会出现更多错误(包括控制器上受保护的 Logged_in? 方法、失败的 RSpec 等等)。

有谁知道,我们是否都在这里做了根本性错误的事情? 或者只是restful_authentication 在当前版本中被破坏了?

I get some of the mentioned errors, too. But the first problem occuring in my app is this:

Multiple step definitions have the same Regexp:

features/step_definitions/user_steps.rb:16:in /^(.*) (.*) user named '(.*)'$/'
features/step_definitions/user_steps.rb:29:in
/^there is no (.*) user named '(.
*)'$/'

(Cucumber::Redundant)

Sure, I can fix this, but many more errors will follow (including the protected logged_in? method on the controller, the failing RSpecs and so on).

Does anyone know, whether we all do somthing fundamentally wrong here? Or is it just that restful_authentication is broken in its current release?

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