简体   繁体   中英

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.

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 .

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM