我一直在爲此奮鬥了兩天,似乎沒有太多的在線幫助。我查看了Typus wiki,示例應用程序和測試,我似乎正在做的事情,但我仍然得到HTTP狀態碼302(重定向),我期望在我的測試中200(成功)。測試Rails控制器繼承Typus
下面是什麼應該是適當的文件(除去無關緊要的東西)
配置/初始化/ typus.rb(軌摹typus:遷移已運行,因爲我有一個admin_users表):
Typus.setup do |config|
# Application name.
config.admin_title = "Something"
# config.admin_sub_title = ""
# When mailer_sender is set, password recover is enabled. This email
# address will be used in Admin::Mailer.
config.mailer_sender = "[email protected]"
# Define paperclip attachment styles.
# config.file_preview = :medium
# config.file_thumbnail = :thumb
# Authentication: +:none+, +:http_basic+
# Run `rails g typus:migration` if you need an advanced authentication system.
config.authentication = :session
# Define user_class_name.
config.user_class_name = "AdminUser"
# Define user_fk.
config.user_fk = "admin_user_id"
# Define master_role.
config.master_role = "admin"
end
的config/typus/admin_user.yml
AdminUser:
fields:
default: first_name, last_name, role, email, locale
list: email, role, status
form: first_name, last_name, role, email, password, password_confirmation, locale
options:
selectors: role, locale
booleans:
status: Active, Inactive
filters: status, role
search: first_name, last_name, email
application: Admin
description: Users Administration
測試/工廠/ admin_users.rb:
Factory.define :admin_user do |u|
u.first_name 'Admin'
u.last_name 'User'
u.email '[email protected]'
u.role 'admin'
u.password 'password!'
u.token '1A2B3C4D5E6F'
u.status true
u.locale 'en'
end
測試/功能/管理/ credits_controller_test.rb:
require 'test_helper'
class Admin::CreditsControllerTest < ActionController::TestCase
setup do
@admin_user = Factory(:admin_user)
@request.session[:admin_user_id] = @admin_user.id
@request.env['HTTP_REFERER'] = '/admin/credits/new'
end
context "new" do
should "be successful" do
get :new
assert_response :success
end
end
end
@ response.body:
<html>
<body>You are being <a href="http://test.host/admin/session/new?back_to=%2Fadmin%2Fcredits%2Fnew">redirected</a>.
</body>
</html>
正如你所看到的,我已經建立了使用admin_user的typus和會話密鑰的admin_user_id。但由於某種原因,測試失敗的是302而不是200.我確定這是因爲我做了一些我只是看不到的錯誤。我還創建了所有這些gist,以防萬一有人喜歡。
編輯2011-05-19 09:58 am中央時間:爲每個請求添加了響應正文文本。
這與Typus並無關係,我可以說,它似乎只與您使用的auth機制有關。我看到你正試圖提供一個包含管理員用戶標識的cookie,但這看起來不太合適。 – 2011-05-19 15:03:15
默認情況下,Rails會話存儲使用cookie存儲,所以你會打開自己的篡改。無論您使用哪種身份驗證機制,都應提供各種加密的令牌。 – 2011-05-19 15:07:00
@BenScheirman,是的,我真的不喜歡這樣做,但它是如何在他們的測試應用程序中執行他們自己的測試,[見這裏的例子](https://github.com/fesplugas/typus/斑點/主/測試/應用程序/控制器/管理/ assets_controller_test.rb)。 – Koby 2011-05-19 15:39:02