1

我最近开始一起使用 Cucumber 和 Subdomain-fu,这让我做了一些可怕的事情。

我有一个曾经看起来像这样的步骤定义:

path = grok_path(path)
get path

在我看来,这很好也很容易。但现在是这样的:

path = grok_path(path)
get "http://#{subdomain}.example.com#{path}"

行得通,但它并不完全干净。有什么更好的方法来做到这一点?

4

2 回答 2

2

为了使步骤定义更具可读性,您可以将这些内容包装在一个小方法中,如下所示:

def subdomained_path(path, subdomain):
    return "http://#{subdomain}.example.com#{path}"
end

这使您可以将步骤定义整理成以下内容:

path = grok_path(path)
get subdomained_path(path, subdomain)

从功能上讲,这两者是等效的(并且同样是 hacky),但我建议的更改至少会使代码看起来更干净一些。如果您有权修改 Net::HTTP.get,则可以修改实际的“get”方法以接受子域参数,使其更加简洁。

于 2009-07-30T20:15:27.700 回答
2

我不确定这在 Cucumber 中的应用效果如何(在此处使用 Shoulda),但是在其他地方尝试了一些建议之后,这似乎可以可靠地工作:

def in_subdomain(str)
  # test.host == default testing domain, feel free to change to match your usage
  @request.host = "#{str}.test.host"
end

然后在打电话之前get,你只需要确保你是in_subdomain('subdomain-fuuuuuu')。这正确设置了 URL current_subdomain,至少(我还没有检查所有内容),重定向不指定子域留在子域中,并且任何重定向到其他子域(或:subdomain => false)仍然设置正确的 redirected_to 值。

例如,这些(高质量的,我相信你可以说出来)测试通过了,并且它们检查了控制器中的 current_subdomain:

should "show on the owner's subdomain" do
  in_subdomain(@user.domain)
  get :show, :id => @user.things.first.id
  assert_response :success
end
should "not show on another users' subdomain" do
  in_subdomain(@random_user.domain)
  get :show, :id => @user.things.first.id
  assert_redirected_to user_url(@random_user, :subdomain => @random_user.domain)
end
should "not show on a non-existent subdomain" do
  in_subdomain("cthulhu-fhtagn")
  get :show, :id => @user.things.first.id
  assert_redirected_to root_url(:subdomain => false)
end
于 2011-09-22T00:54:20.120 回答