5

我目前正在努力从Cucumber中删除我的Sinatra应用程序的辅助方法。

我有一个带有简单会话身份验证(通过 cookie)的Sinatra应用程序,我想通过为我的Cucumber场景删除logged_in?辅助方法来关闭身份验证。Sinatra 和 Cucumber 似乎在会话方面存在问题,所以我考虑只使用 Mocha 来解决这个问题。

但是我不知道如何Sinatra::ApplicationGiven-Block 中访问该实例以存根该方法。

4

2 回答 2

3

似乎我需要在Before do ... end-block中直接覆盖我的身份验证机制

所以我最终hooks.rbfeatures/support/文件中覆盖了我logged_in?的和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

我唯一需要注意的是,应用程序中没有其他操作直接读取,session而是使用这些助手。

可悲的是,我认为这种通过 Cucumber 处理基于会话的 Sinatra 应用程序的方式已经在其他地方进行了描述,我只是认为我的问题有所不同

于 2009-11-26T09:58:21.673 回答
2

您可以通过使用获得正确的上下文Sinatra::Application.class_eval

编辑:有关完整解释,请参阅原始海报的答案。

于 2009-11-25T23:38:13.333 回答