我的應用程序使用Devise進行身份驗證。我想編寫集成規格來進行測試,以防止正確的身份驗證和訪問預防。通過填寫表格進行集成測試的水豚認證?
不知何故,這兩者似乎並沒有很好地協同工作。在the devise repo,自述說,這對sign_in和SIGN_OUT助手是設計給你測試:
這些傭工不會通過水豚或Webrat驅動的集成測試工作。它們只能用於功能測試。相反,填寫表格或明確設置在會話的用戶
所以我想要做的驗證是填寫表單。 我寫這個(spec/support/signin_helpers.rb
):
module SignInHelpers
def sign_in(user)
visit users_login_path
fill_in "Email", with: user.email
fill_in "Passwort", with: "rickroll"
click_button "Einloggen"
end
def login_admin
before(:each) do
sign_in FactoryGirl.create(:admin) # Using factory girl as an example
end
end
def login_user
before(:each) do
user = FactoryGirl.create(:user)
sign_in user
end
end
end
而且我的測試是這樣的:
describe "unauthorized access" do
login_user
describe "to Companies#new" do
before { get new_company_path }
specify { response.should redirect_to(root_path) }
end
.
.
.
end
這似乎工作本身,完全沒有問題。沒有「真實」的錯誤拋出。但不知何故,在某處,認證丟失:
5) CompaniesManagement unauthorized access to Companies#index should redirect to "/"
Failure/Error: specify { response.should redirect_to(root_path) }
Expected response to be a redirect to <http://www.example.com/> but was a redirect to <http://www.example.com/users/login>
# ./spec/requests/companies_management_spec.rb:60:in `block (4 levels) in <top (required)>'
我在做什麼錯了?
我不知道這是否解決您的問題,但'login_user'應該在'before'塊。 – gotva