如何让故事与宁静的身份验证和黄瓜一起工作?

时间:2009-03-25 01:44:32

标签: ruby-on-rails rspec cucumber restful-authentication rspec-stories

克隆最新稳定版

进入一个干净的rails应用程序,并遵循(我相信的)每个插件的所有说明,黄瓜故事仍然失败:-(。这里是问题的摘要:

    尽管创建了'map.root:controller =>,但
  1. 重定向仍无法正常工作。 “my_controller”'路线:
    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. 故事说logged_in?方法受到保护,尽管features/step_definitions/ra_env.rb文件调用:
    ApplicationController.send(:public, :logged\_in?, :current\_user, :authorized?)
    这个调用是否可以在不需要存根的情况下使这些方法可用?
  3. 哦,我正在尝试运行autospec,所以我已经完成了以下命令来启动它:

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

7 个答案:

答案 0 :(得分:2)

我做了一些研究,这就是我得到的。 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

注意if internal_redirect? ... end。这个if是使我们的测试失败的因为webrat正在遵循重定向。 作为一种解决方法,您可以在webrat会话上对这些行进行注释,但这可能不是一个合适的解决方案。我会工作一点,并在某处发布补丁。

答案 1 :(得分:2)

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

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

基本上,restful身份验证测试试图测试不应该使用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来自哪里。其他人有类似的错误吗?

答案 2 :(得分:1)

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

def log_out
得到'/ logout'
结束

在尝试获取'/ session / destroy'之前,只有在你不删除默认路由时才存在。

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

虽然仍然在解决其他一些问题......

答案 3 :(得分:1)

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

转动config.cache_classes = false会再次引入错误。

似乎问题在于如何在rails中实现类缓存,或者rspec如何管理已创建的类。不幸的是,我没有足够的资源来调查整个日志,并且似乎有一个很好的讨论,它发生在: http://groups.google.com/group/rspec/browse_thread/thread/500ede090bd08996/25a3d9a7d283696b?lnk=gst&q=cache_classes#25a3d9a7d283696b

答案 4 :(得分:0)

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

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

最后,一旦我弄清楚要去哪里,大多数失败都很容易解决。

答案 5 :(得分:0)

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

答案 6 :(得分:0)

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

多个步骤定义具有相同的Regexp:

features / step_definitions / user_steps.rb:16:在/^(.*) (.*) user named '(.*)'$/' features/step_definitions/user_steps.rb:29:in / ^中没有(。*)用户名为'(。 *) '$ /'

(黄瓜::冗余)

当然,我可以修复此问题,但会出现更多错误(包括控制器上受保护的logged_in?方法,失败的RSpec等等)。

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