Sinatra's stunning assistant in a cucumber

I am currently trying my best to use the helper method of my Sinatra application from Cucumber .

I have an application Sinatra with a simple authentication session (via cookie files), and I want to enable authentication by following helper method logged_in?for scenarios Cucumbers , I think that the problem with Sinatra and Cucumber associated with meetings, so I thought about just using Mocha to solve this problem.

However, I do not know how I can access the instance Sinatra::Applicationfrom Given-Block to stub the method.

+5
source share
2 answers

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

Edit: See the original answer to the poster for a full explanation.

+2
source

It seems I need to directly override the authentication mechanism in Before do ... end-block

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

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 needed to take care of was that no other actions in the application are directly read from session, but these helpers use.

Unfortunately, this way of handling Sinatra session applications through Cucumber has already been described elsewhere, and I just thought my problem was different.

+3
source

All Articles