繁体   English   中英

在 Cucumber 中存根 Sinatra 助手

[英]Stubbing Sinatra helper in Cucumber

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

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

但是,我不知道如何从Given -Block 中访问Sinatra::Application实例来存根该方法。

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

所以我最终在features/support/文件中放置了一个hooks.rb覆盖了我的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 应用程序的方式已经在其他地方进行了描述,我只是认为我的问题有所不同

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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