1
我試圖弄清楚爲什麼這不起作用。Rails:功能測試中的工廠和協會
假設你有三個模型,User,Foo和Bar。爲了創建條形圖,用戶必須首先創建並驗證foo對象。
Class User #snip!
has_many :foos
has_many :bars
Class Foo #snip!
belongs_to :user
has_many :bars
Class Bar #snip!
belongs_to :user
belongs_to :foo
我試圖得到一個功能測試工作,如果用戶試圖作出新的酒吧,而無需有效foo其中,他們重定向到一個富的「新」行動。
我對重定向方案沒有任何問題。但是,當我嘗試使用有效的Foo對象設置用戶並嘗試獲取Bar的「新」操作時,它仍然被重定向到Foo控制器的「新」操作。它仍然不承認用戶擁有Foo。
這裏是我的控制器:
class BarsControllerTest < ActionController::TestCase
setup :activate_authlogic
def setup
@request.env['HTTPS'] = nil
@user = Factory.build(:user)
@foo = Factory.build(:foo, :user => @user)
end
test "should get new when user has a valid foo" do
@request.env['HTTPS'] = 'on'
UserSession.create(@user)
get :new
assert_response :success
end
這是重定向功能我在我的應用程序控制器,它被稱爲我的吧控制器:
def foo_required
if current_user && @current_user.foos.valid.empty? && @current_user.foos.empty?
flash[:notice] = "You must have a verified foo in order to create a Bar!"
redirect_to new_foo_path
elsif current_user && @current_user.foos.valid.empty?
flash[:notice] = "You must verify your foos in order to create a Bar!"
redirect_to foos_path
end
end
這裏的富廠:
Factory.define :foo do |f|
#attributes
f.valid true
f.association :user
end
而是我被重定向到「https://test.host:80/foos/new」控制器沒有知道用戶有一個Foo ...
會話是有效的,所以這似乎是一個工廠的問題,但我不知道它是什麼。
當然,它必須是這樣的簡單! *擊中頭部*非常感謝jdl,你爲我挽回了一天的拉發! – 2010-02-20 02:02:27
不客氣。 – jdl 2010-02-20 03:51:45