ฉันต้องการทดสอบวิธีนี้ในคอนโทรลเลอร์ของฉัน
def fetch_match_displayed_count(params)
match_count = 0
params.each do |param|
match_count += 1 if param[1]["result"] && param[1]["result"] != "result"
end
match_count
end
นี่คือแบบทดสอบที่ฉันเขียนจนถึงตอนนี้
describe "fetch_match_displayed_count" do
it "should assign match_count with correct number of matches" do
params = {"result_1"=>{"match_id"=>"975", "result"=>"not_match"}, "result_2"=>{"match_id"=>"976", "result"=>"match"}, "result_3"=>{"match_id"=>"977", "result"=>"not_sure"}, "result_4"=>{"match_id"=>"978", "result"=>"match"}, "result_5"=>{"match_id"=>"979", "result"=>"not_match"}, "workerId"=>"123", "hitId"=>"", "assignmentId"=>"", "controller"=>"mt_results", "action"=>"create"}
controller.should_receive(:fetch_match_displayed_count).with(params)
get :fetch_match_displayed_count, {params:params}
assigns(:match_count).should == 5
end
end
ปัญหาของฉันดูเหมือนจะอยู่ในบรรทัดนี้ get :fetch_match_displayed_count, {params:params}
วิธีการนี้คาดว่าจะมีพารามิเตอร์ แต่จะไม่ได้รับศูนย์
ฉันมีสองคำถาม
วิธีการนี้ควรอยู่ในตัวช่วยและไม่ได้อยู่ในตัวควบคุม (ตามแบบแผน Rails) หรือไม่
ฉันจะส่งคำขอรับและผ่าน
params
ในการทดสอบได้อย่างไร
create
จากนั้น ฉันสามารถคาดหวังสิ่งต่อไปนี้:controller.should_receive(:fetch_match_displayed_count).with(params).and_return(5)
- person Huy   schedule 01.02.2013