添加 Subdomain-fu 和重定向功能后 Cucumber/webrat 测试失败
我在我的项目中添加了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以看出 Webrat 非常喜欢域名“example.com”并模仿它的所有步骤。
因此,如果您不使用子域,则可以在步骤中写入:
并且所有重定向都应该有效(我不检查)。
有了 Subdomain-fu 就更简单了。您应该做的一切 - 正确配置您的 subdomain-fu 配置文件:
并在没有任何主机的情况下进行重定向测试! “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:
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:
and tests with redirection work without any host! "example.com".