2012-11-21 30 views
8

我正在嘗試學習黃瓜以及如何正確使用它。在尋找最佳實踐時,大多數舊方法都有描述,而且我還沒有找到一個好的指導。 我閱讀了關於這種方法的新方法,但我在最佳實踐中遇到了一些問題。如何爲鐵軌寫黃瓜(最佳做法)。功能和步驟

以下是我一直在努力的一些基本的黃瓜場景。

Scenario: Unsuccessful login 
    Given a user has an account 
    When the user tries to log in with invalid information 
    Then the user should see an log in error message 

Scenario: Successful login 
    Given a user has an account 
    When the user logs in 
    Then the user should see an log in success message 
    And the user should see a sign out link 

Scenario: Successful logout 
    Given a signed in user 
    Then the user logs out 
    And the user should see an log out success message 

我不知道這是否好嗎?如果我把它寫成「我訪問」或「用戶訪問」或「他訪問」,我有問題基本上哪些是首選?

其次我不知道我應該怎麼制定如下:

Scenario: Visit profile of user 
    Given a user 
    And a second user 
    When the user visit the user profile 
    Then the user should see the name of the user 

Scenario: Visit profile of another user 
    Given a user 
    And a second user 
    When the user visit the second users profile 
    Then the user should see the name of the second user 

這只是我放在一起,但我覺得這不是最好的辦法。 我遇到了我的步驟定義文件中的問題。你將如何定義處理場景的步驟?我想寫更一般的東西,但也許這不是真的可能?我應該擁有@second_user屬性還是你有什麼建議?

def user 
    @user ||= FactoryGirl.create :user 
end 

Given /^a signed in user$/ do 
    user 
    sign_in(@user.email, @user.password) 
end 

Given /^a user has an account$/ do 
    user 
end 


When /^the user logs in$/ do 
    sign_in(@user.email, @user.password) 
end 

When /^the user logs out$/ do 
    click_link ('Sign out') 
end 

When /^the user tries to log in with invalid information$/ do 
    sign_in("incorrect-email", "incorrect-password") 
end 

Then /^the user should see a sign out link$/ do 
    page.should have_link('Sign out') 
end 

Then /^the user should see an log in success message$/ do 
    should have_success_message('Signed in successfully.') 
end 

When /^the user should see an log out success message$/ do 
    should have_success_message('Signed out successfully.') 
end 

Then /^the user should see an log in error message$/ do 
    should have_error_message('Invalid email or password.') 
end 

感謝您的幫助!

回答

4

我有問題,如果我應該寫爲「我訪問」或「用戶訪問」或「他訪問」基本上什麼是首選?

我想使用類似「當用戶訪問」將是更通用的,將是更具可讀性,你不必老想着「誰是‘他’?」如果你正在閱讀測試,真正重要的是你在所有的文件中堅持一些約定,這樣你就不會感到困惑。

關於創建@second_user的第二個問題,我認爲你不應該這樣做,因爲這個用戶並不是你的場景的一部分,而是一個數據設置,我認爲處理數據的最好方法設置是使用pickle with cucumber,它基本上允許你在你的黃瓜中創建模型,而不必將它們作爲你的變量,有一個great cast on RailsCast解釋了很多。

,所以我會做到這一點使用泡菜

Scenario: Visit profile of another user 
    Given a user exists with name: "Mike", username: "mike" 
    And a signed in user 
    When the user visits the profile of "mike" 
    Then the user should see 'Mike' 

則你可以定義

When /^the user visits the profile of (.+)$/ do |username| 
    visit("/#{username}") # I am assuming here usernames are unique and the profile url is "/:username" 
end 
+0

感謝。似乎沒有辦法在模型和黃瓜特徵之間添加一些耦合。我的希望是沒有任何這樣的信息像用戶名:在我的功能,但似乎沒有好的方法。 –

+1

不客氣,我認爲沒有錯誤,如果有耦合,因爲這就是我們編寫測試的原因。例如,如果您更改用戶模型而沒有用戶名字段,那麼這種情況就會失敗,這是應該發生的事情,然後你會修復模型或改變場景。 – Khaled