我正在尝试测试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?
答案 0 :(得分:1)
我会将整个搜索逻辑放入Product模型并将params hash传递给它。
然后我会创建一个方法来在模型规范中测试它。这应该是非常直接的。
您遇到的问题是什么?