Rails rspec 设置子域
我正在使用 rSpec 来测试我的应用程序。在我的应用程序控制器中,我有一个像这样的方法:
def set_current_account
@current_account ||= Account.find_by_subdomain(request.subdomains.first)
end
Is it possible to set the request.subdomain in my spec?也许在之前的块中?我是 rSpec 的新手,所以对此的任何建议都会非常感谢。
埃夫
I am using rSpec for testing my application. In my application controller I have a method like so:
def set_current_account
@current_account ||= Account.find_by_subdomain(request.subdomains.first)
end
Is it possible to set the request.subdomain in my spec? Maybe in the before block? I am new to rSpec so any advice on this would be great thanks.
Eef
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我想出了如何解决这个问题。
在我的规范中的 before 块中,我简单地添加了:
这将 request.subdomains.first 设置为 mock_subdomain 的值。
希望有人发现这很有用,因为它在网络上的其他地方没有得到很好的解释。
I figured out how to sort this issue.
In my before block in my specs I simply added:
This setups up the request.subdomains.first to be the value of the mock_subdomain.
Hope someone finds this useful as its not explained very well anywhere else on the net.
我知道这是一个相对较老的问题,但我发现这取决于您正在运行的测试类型。我还运行 Rails 4 和 RSpec 3.2,所以我确信自从提出这个问题以来,有些事情已经发生了变化。
请求规格
Capybara 的功能规格
我通常在
spec/support
中创建如下所示的模块:包含在
spec/rails_helper 中。 rb
:然后你可以在你的规范中调用,如下所示:
I know this is a relatively old question, but I've found that this depends on what kind of test you're running. I'm also running Rails 4 and RSpec 3.2, so I'm sure some things have changed since this question was asked.
Request Specs
Feature Specs with Capybara
I usually create modules in
spec/support
that look something like this:Include in
spec/rails_helper.rb
:Then you can call within your spec like so:
在 Rails 3 中,我尝试手动设置主机的所有操作都不起作用,但是查看代码,我注意到它们很好地解析了您传递给请求帮助程序(例如
get
)的路径。如果您的控制器获取子域中提到的用户并将其存储为
@king_of_the_castle
,那就足够了In rails 3 everything I tried to manually set the host didn't work, but looking the code I noticed how nicely they parsed the path you pass to the request helpers like
get
.Sure enough if your controller goes and fetches the user mentioned in the subdomain and stores it as
@king_of_the_castle
Chris Peters 的答案对我的请求规范有用,但对于功能规范,我必须进行以下更改:
rails_helper:
feature_subdomain_helpers:
Chris Peters' answer worked for me for Request specs, but for Feature specs, I had to make the following changes:
rails_helper:
feature_subdomain_helpers: