添加 Subdomain-fu 和重定向功能后 Cucumber/webrat 测试失败

发布于 2024-08-15 22:54:49 字数 1160 浏览 3 评论 0原文

我在我的项目中添加了 Subdomain-fu 。在ApplicationController中,我有before_filter,它检查url并将app.com重定向到www.app.com,将www.subdomain.app.com重定向到subdomain.app.com并检查帐户是否存在(如果不存在则重定向到home):

    before_filter :check_uri 

    def check_uri
      if !subdomain?
        redirect_to http_protocol + "www." + current_host + request.request_uri if !/^www\./.match(current_host)
      elsif /^www\./.match(current_host)
        redirect_to http_protocol + current_host.gsub(/^www\./, '') + request.request_uri
      elsif !account_subdomain?
        redirect_to http_protocol + "www" + current_host.gsub(account_subdomain, '')
      end
    end

上面的代码工作得很好好的。但在添加此片段后,我的黄瓜测试,例如。这个:

  Scenario: Successful sign up
    Given I am an anonymous user
    And an Accept Language header
    And I am on the home page
    When I follow "Join now!"
    And ...

因错误而失败:

Webrat::NotFoundError: Could not find link with text or title or id "Join now!"
(eval):2:in `/^I follow "([^\"]*)"$/'
features/manage_users.feature:10:in `When I follow "Join now!"'

如果我在 before_filter 上评论,一切都会正常。 有人知道为什么吗?

I added Subdomain-fu in my project. In ApplicationController I have before_filter which checks url and redirects app.com to www.app.com, www.subdomain.app.com to subdomain.app.com and checks account existence (redirects to home if not exists):

    before_filter :check_uri 

    def check_uri
      if !subdomain?
        redirect_to http_protocol + "www." + current_host + request.request_uri if !/^www\./.match(current_host)
      elsif /^www\./.match(current_host)
        redirect_to http_protocol + current_host.gsub(/^www\./, '') + request.request_uri
      elsif !account_subdomain?
        redirect_to http_protocol + "www" + current_host.gsub(account_subdomain, '')
      end
    end

Code above works pretty nice. But after adding this snippet my Cucumber tests, for ex. this one:

  Scenario: Successful sign up
    Given I am an anonymous user
    And an Accept Language header
    And I am on the home page
    When I follow "Join now!"
    And ...

became fail with error:

Webrat::NotFoundError: Could not find link with text or title or id "Join now!"
(eval):2:in `/^I follow "([^\"]*)"$/'
features/manage_users.feature:10:in `When I follow "Join now!"'

If I comment this before_filter everything works well.
Does anybody know why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甲如呢乙后呢 2024-08-22 22:54:49

我可以看出 Webrat 非常喜欢域名“example.com”并模仿它的所有步骤。

因此,如果您不使用子域,则可以在步骤中写入:

Before do
  host! "example.com"
end

并且所有重定向都应该有效(我不检查)。
有了 Subdomain-fu 就更简单了。您应该做的一切 - 正确配置您的 subdomain-fu 配置文件:

SubdomainFu.tld_sizes = {:development => 0,
                         :test => 1, # not 0, even if you use *.localhost in development
                         :production => 1}

并在没有任何主机的情况下进行重定向测试! “example.com”。

I could figure out that Webrat loves domain "example.com" very deeply and emulates all step with it.

So if you don't use subdomains, you can write in your steps:

Before do
  host! "example.com"
end

and all your redirections should work (I don't check).
With Subdomain-fu it's even esier. Everything what you should do - to configure your subdomain-fu config file properly:

SubdomainFu.tld_sizes = {:development => 0,
                         :test => 1, # not 0, even if you use *.localhost in development
                         :production => 1}

and tests with redirection work without any host! "example.com".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文