1

我正在尝试测试 Rspec 的搜索功能,
这是我到目前为止所拥有的,

def product_search
    filter = ""
    filter = filter + " and B.sku like '#{params[:sku]}%'" if params[:sku]  && params[:sku] != ""
    filter = filter + " and A.name like '#{params[:name]}%'" if params[:name] && params[:name] != ""
    filter = filter + " and D.name like '#{params[:store_name]}%'" if params[:store_name] && params[:store_name] != ""
    if params[:approved] == "0"
        filter= filter + "and (A.is_approved = false And A.approved_by is NULL) "
    elsif params[:approved] == "1"
        filter= filter + "and A.is_approved = true "
    elsif params[:approved] == "2"
        filter= filter + " and (A.is_approved = false And A.approved_by is NOT NULL) "
    end
  @products=Product.seller_product_search(filter,current_user.store.id).paginate(:page => params[:page], :per_page => 10)
  respond_to do |format|
  format.html
  format.js 
    end
end

我应该如何为这段代码编写 rspec?

4

1 回答 1

1

我会将整个搜索逻辑放入 Product 模型中并将参数哈希传递给它。

然后我会创建一个方法来在模型规范中测试它。这应该很简单。

您遇到什么问题?

于 2012-04-10T19:34:40.090 回答