使用open_id_authentication插件时,如何在RSpec用户故事/ Cucumber中伪造OpenID登录

我正在尝试编写一个Cucumber场景,要求我有一个登录用户 – 这通常很简单,但我只使用OpenID身份validation(认证插件的简化)。 然而,在深入了解open_id_authentication插件后,我不确定如何在Cucumber中实现这一点。

我已经找到了一种方法,如果你将它放在你的features / support / env.rb中:

ActionController::Base.class_eval do private def begin_open_id_authentication(identity_url, options = {}) yield OpenIdAuthentication::Result.new(:successful), identity_url, nil end end 

然后你可以在适当的步骤中做这样的事情:

 Given /^I am logged in as "(.*)"$/ do |name| user = User.find_by_name(user) post '/session', :openid_url => user.identity_url # Some assertions just to make sure our hack in env.rb is still working response.should redirect_to('/') flash[:notice].should eql('Logged in successfully') end 

我只是完全破坏了黄瓜function的开放id身份validation,显然如果我需要登录失败的实例,我可以根据提供的identity_url做到这一点。

如果您希望能够存根响应,请执行以下操作:

在features / support / helpers.rb中:

 ActionController::Base.class_eval do private def fake_openid_response(identity_url) [OpenIdAuthentication::Result.new(:successful), identity_url, nil] end def begin_open_id_authentication(identity_url, options = {}) yield fake_openid_response(identity_url) end end 

通过将响应移到单独的方法,您现在可以根据需要在步骤中存根响应。 例如,如果我想要:缺少响应并且我有一个控制器GoogleLoginController我可以使用Mocha执行以下操作:

 GoogleLoginController.any_instance.stubs(:fake_openid_response) .returns([OpenIdAuthentication::Result.new(:missing), identity_url, nil]) 

Bort ,一个rails骨架应用程序,有一整套rspec测试并支持openID登录,所以你可能想看看他们做了什么。

除了我需要规范化identity_url之外,DEfusion的答案是有效的:

 ActionController::Base.class_eval do private def begin_open_id_authentication(identity_url, options = {}) yield OpenIdAuthentication::Result.new(:successful), self.normalize_identifier(identity_url), nil end end 

谢谢