2008-12-19 77 views

回答

4

,我想出了一個辦法,如果你把這個在你的功能/支持/ 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 

我只是完全打開了黃瓜功能的開放id auth,顯然如果我需要那些登錄失敗的實例,我可以根據提供的identity_url來做到這一點。

0

DEfusion的答覆工作,只是我需要正常化像identity_url:

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 

感謝

2

Bort,一個軌道骨架的應用程序,擁有全套的RSpec的測試和支持OpenID登錄,所以你可能想看看他們做什麼。

2

如果你希望能夠存根出反應做到這一點:

在功能/支持/ 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 

通過移動了一個單獨的方法,你現在可以存根響應必要時在你的步驟中做出迴應。例如,如果我想要a:缺少響應,並且我有一個控制器GoogleLoginController,則可以使用Mocha執行以下操作:

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