我有一個看起來像這樣的請求規格:更好的方式來編寫這個請求規範?
describe "Registering from the main page" do
before(:each) do
fill_in "username","my_username"
fill_in "password","my_password"
fill_in "password_confirmation","my_password"
click_button "Register"
end
it "should have created a new user" do
User.count.should == 1
end
it "should have changed the message on the homepage" do
find("#element").value.should == "You registered!"
end
# ... other tests like this
end
第一個問題,我怎麼能保證我有0用戶提交表單之前?
其次,before
對我來說看起來不太合適,即使它能完成這項工作。是否有另一種方式來編寫整個註冊用例的測試?或許以某種方式重新構思前一部分,還是以我的方式接受?
更新:我將before(:all)
更改爲before(:each)
,因爲測試開始失敗。當我使用before(:all)
時,響應的主體似乎不完整。你碰巧知道爲什麼?
怎麼樣的事實,這是一個'前(:每個)'塊?有沒有辦法讓它離開那裏? – Geo 2012-04-06 12:00:14
除了'click_button'這一行註冊「',我認爲代碼是在正確的地方。它是測試註冊的準備,看起來它需要在所有示例中都存在,所以'before(:each)'很好。 – tsherif 2012-04-06 12:06:08
另外,增加了一些東西來解決'before(:all)'/'before(:each)'問題。 – tsherif 2012-04-06 12:16:03