試圖讓這個功能測試通過:Rails的功能測試利用需要的兒童
test "should create question" do
assert_difference('Question.count') do
post :create, :question => @question.attributes
end
end
但@question有需要特定的孩子驗證是存在明確的一個課題:
class Question < ActiveRecord::Base
has_many :topic_questions
has_many :topics, :through => :topic_questions
validate :has_topic
def has_topic
(errors[:base] << "You must have one topic") if (topics.count < 1)
end
end
會如何我1)在測試中爲@question構建主題,然後2)將它傳遞給post方法,因爲它不會被.attributes()函數傳遞?
有沒有辦法使用以下方法做同樣的事情? `後:創建,問題:{主題:@a_topics_array}` – 2013-01-07 20:21:41