2011-06-01 128 views
1

我知道理想的做法是填寫登錄表單並遵循該流程。問題是我沒有使用設計進行登錄。我使用Facebook和fb_graph gem進行身份驗證後,在我的應用程序中登錄了用戶。測試設計用黃瓜登錄

因此,設計的sign_in視圖只有鏈接「連接Facebook」,但我能夠看到該路線,並且我假設在用戶登錄該網址時會嘗試登錄該用戶。

我試圖做一個郵寄到sign_in(這觀點是空的)直接用黃瓜,和即使響應是確定的,用戶沒有登錄。

Given /^I am a logged in user$/ do 
    @user = Factory(:user) 
    res = post("https://stackoverflow.com/users/sign_in", :email => @user.email, :password => "password") 
    p res 
end 

如何測試呢?

感謝,

UPDATE:

的情況是這樣的:

Scenario: Going to the index page 
    Given I am a logged in user 
    And there is a subject created 
    And there is 1 person for that subject 
    When I go to that subject persons index page 
    And show me the page 
    Then I should see "Back to Subjects list" 

回答

2

而是這樣做的,這我不是我驕傲落得這樣做如下:

應用控制器

before_filter :authenticate_user!, :except => [:login] 

# This action is supposed to only be accessed in the test environment. 
# This is for being able of running the cucumber tests. 
def login 
    @user = User.find(params[:id]) 
    sign_in(@user) 
    current_user = @user 
    render :text => "user logged in" 
end 

路線

# This is for being able of testing the application with cucumber. Since we are not using devise defaults login 
match 'login/:id' => 'application#login', :as => 'login', :via => [:get] if Rails.env.test? 

使用者步驟

Given /^I am a logged in (student|employee)+ user$/ do |role| 
    @user = @that = Factory(:user, :role => role, :name => "#{role} User Name") 
    Given("that user is logged in") 
end 

Given /^that user is logged in$/ do 
    Given("I go to that users login page") 
end 

路徑

when /that users login page/ 
    login_path(@that || @user) 

這樣,在我的情況下,我只需要鍵入:

Given I am a logged in student user 

,其餘的只是正常的黃瓜......

0

我不得不說,這是我想出了一些討厭的猴子補丁。

添加到我的application_controller。

if Rails.env.test? 
    prepend_before_filter :stub_current_user 
    # UGLY MONKEY PATCH. we need a current user here. 
    def stub_current_user 
    unless user_signed_in? 
     @user = Factory(:user) 
     sign_in(@user) 
     current_user = @user 
    end 
    end 
end 

記住我的應用程序中沒有sign_in表單,而我正在使用devise。也許以後,我會嘗試尋找更好的方法,但現在,這讓我完成了一些事情。