views:

290

answers:

2

I am currently struggling with stubbing out a helper method of my Sinatra app from within Cucumber.

I have a Sinatra app with simple session authentication (by cookies) and I want to turn of authentication by stubbing out the logged_in? helper method for my Cucumber scenarios. There seems to be a problem with Sinatra and Cucumber concerning sessions so I thought about just using Mocha to work around the problem.

However I don't know how I can access the Sinatra::Application instance from within a Given-Block to stub out the method.

+1  A: 

You can get the right context by using Sinatra::Application.class_eval

Edit: See original poster's answer for full explanation.

Andy West
Sorry, I think you misunderstood my question. The logged_in? method already works within the sinatra app, but I want to stub it out for integration testing (no authentication to test certain workflows). The main problem is that I don't know how to access the instance of my application from within Cucumber steps.
Lennart
I see. I think I understand now. Maybe something with Sinatra::Application.class_eval { }
Andy West
yup, this made it work. Thanks, how can I give you props for that hint? I'll answer my own question to clarify what I've done.
Lennart
Post was edited to reflect correct answer.
Andy West
Ah, I see what you mean. My answer is not complete. You could just upvote it and mark yours as correct. That seems like the right thing.
Andy West
+1  A: 

It seems like I need to directly override my Authentication mechanism within a Before do ... end-block

So I ended up with a hooks.rb placed in features/support/ file overwriting my logged_in? and the current_user method.

Before do
  MySinatraApplicationClass.class_eval do
    helpers do
      def logged_in?
        return true
      end
      def current_user
        # This returns a certain Username usually stored 
        # in the session, returning it like
        # that prohibits different user logins, but for
        # now this is enough for me
        "Walter"
      end
    end
  end
end

The only thing I had to take care of, is that the no other actions within the application directly read from session but rather use those helpers.

Sadly I think this way of handling session based Sinatra applications through Cucumber is already described somewhere else and I just thought my problem was different.

Lennart