Tutorial :How to get stories to work with restful_authentication and cucumber?



Question:

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  


Solution:1

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.


Solution:2

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?


Solution:3

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...


Solution:4

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


Solution:5

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.


Solution:6

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.


Solution:7

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?


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »