1

我想在我的控制器中测试这个方法。

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} 该方法期望参数,但结果为零。

我有两个问题。

  1. 该方法是否应该在帮助程序中而不是在控制器本身中(根据 Rails 约定)?

  2. 如何提交获取请求并通过params测试?

4

1 回答 1

1

作为一般规则,您应该测试类的公共接口。对于控制器,这意味着您测试操作,而不是辅助方法。

您应该能够通过设置一个或多个调用适当操作的单独测试用例来完成这项工作,然后使用消息期望来测试是否使用正确的参数调用了辅助方法 - 或测试辅助方法方法做它应该做的(设置正确的实例变量/重定向/等)。

于 2013-02-01T02:41:39.737 回答