4
我的問題是我必須爲每個水豚測試創建一個新用戶並登錄。在使用水豚的測試之間保持數據庫中的用戶?
下面是一個例子:
require 'spec_helper'
describe "users" do
describe "user registration" do
it "should create a new user and log in" do
# Register a new user to be used during the testing process
visit signup_path
fill_in 'Email', with: 'testuser'
fill_in 'Password', with: 'testpass'
fill_in 'Password confirmation', with: 'testpass'
click_button 'Create User'
current_path.should == root_path
page.should have_content 'Thank you for signing up!'
end
end
describe "user login" do
it "should log in" do
# log in
visit login_path
fill_in 'Email', with: 'testuser'
fill_in 'Password', with: 'testpass'
click_button 'Log In'
current_path.should == root_path
page.should have_content 'Logged in!'
end
end
end
登錄測試失敗,因爲用戶不再在數據庫中爲測試是否存在。
這可以通過將兩者都放在一個測試中來解決,但我認爲這是不好的做法。
另外我還有另一個文件,目前正在使用before_do在每次測試之間註冊和登錄,這似乎也相當糟糕......你can see that code here。
記錄這是我的第一個rails應用程序,所以也許我試圖做錯誤的方式。我想盡可能地把它弄乾。
水豚真的不好用在需要用戶登錄的頁面上嗎?
謝謝這是非常有用的:) –