【发布时间】:2011-09-16 13:50:37
【问题描述】:
我们在 Rails 应用程序中几乎所有基于表格的视图中都有一个带有过滤器的栏,我们需要测试控制器操作。
代码示例:
def index
@users = User.
with_status(params[:status]).
with_role(params[:role_id]).
search(params[:q])
end
上述方法是 ActiveRecord 范围,如果传递的值为空,则设置为绕过。
我现在需要做的是合理地规范它并测试所有 esge 案例:
- 没有传递参数
- 仅角色、仅状态、仅搜索
- 角色 + 状态、角色 + 搜索、...(2 对)
- 角色 + 状态 + 搜索
我写的基本规范示例如下:
context "when filtering by status" do
before do
1.times { Factory(:user, :status => "one") }
3.times { Factory(:user, :status => "other") }
end
it "returns only users with the provided :status" do
get :index, :status => "one"
assigns(:users).size.should == 1
assigns(:users)[0].status.should == "one"
end
end
我想编写一个矩阵来混合和匹配角色、状态和搜索参数并生成适当的规范示例。
Array#permutation 是解决方案还是有更好的方法?
【问题讨论】:
-
我会通过排列来做,但我也有兴趣看看是否有人知道更好的方法。
-
我只是用排列完成了它,即使我管理了一个很好的行/规格比(对于 60 多个规格大约 10 行),我担心它的复杂性。
-
感谢分享。如果我找到更好的方法,我会在这里发布。