Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stubbing Sinatra helper in Cucumber

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.

like image 453
Lennart Avatar asked Nov 25 '09 22:11

Lennart


2 Answers

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.

like image 144
Lennart Avatar answered Nov 20 '22 07:11

Lennart


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

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

like image 25
Andy West Avatar answered Nov 20 '22 07:11

Andy West