我正在嘗試學習黃瓜以及如何正確使用它。在尋找最佳實踐時,大多數舊方法都有描述,而且我還沒有找到一個好的指導。 我閱讀了關於這種方法的新方法,但我在最佳實踐中遇到了一些問題。如何爲鐵軌寫黃瓜(最佳做法)。功能和步驟
以下是我一直在努力的一些基本的黃瓜場景。
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
感謝您的幫助!
感謝。似乎沒有辦法在模型和黃瓜特徵之間添加一些耦合。我的希望是沒有任何這樣的信息像用戶名:在我的功能,但似乎沒有好的方法。 –
不客氣,我認爲沒有錯誤,如果有耦合,因爲這就是我們編寫測試的原因。例如,如果您更改用戶模型而沒有用戶名字段,那麼這種情況就會失敗,這是應該發生的事情,然後你會修復模型或改變場景。 – Khaled