我目前正在努力从 Cucumber 中删除我的 Sinatra 应用程序的辅助方法。
我有一个带有简单会话身份验证的 Sinatra 应用程序(通过Cookie),我希望通过为我的 Cucumber logged_in?帮助程序方法来启用身份验证>场景。 Sinatra和Cucumber似乎有关于会话的问题,所以我想到只使用Mocha来解决这个问题。
但是我不知道如何从Sinatra::Application
中访问Given
实例 - 阻止该方法的存根。
答案 0 :(得分:3)
似乎我需要在Before do ... end
- 块
所以我最后在hooks.rb
文件中放置了features/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应用程序的方式已经在其他地方进行了描述,我只是认为我的问题不同。
答案 1 :(得分:2)
您可以使用Sinatra::Application.class_eval
编辑:请参阅原始海报的答案以获得完整的解释。