Rai*_*Son 57 subdomain specifications rspec ruby-on-rails
我正在使用rSpec来测试我的应用程序.在我的应用程序控制器中,我有一个这样的方法:
def set_current_account
@current_account ||= Account.find_by_subdomain(request.subdomains.first)
end
Run Code Online (Sandbox Code Playgroud)
是否可以在我的规范中设置request.subdomain?也许在之前的街区?我是rSpec的新手所以对此有任何建议都非常感谢.
EEF
Rai*_*Son 88
我想出了如何排序这个问题.
在我之前的块中,我只是添加了:
before(:each) do
@request.host = "#{mock_subdomain}.example.com"
end
Run Code Online (Sandbox Code Playgroud)
这会将request.subdomains.first设置为mock_subdomain的值.
希望有人觉得这很有用,因为它在网上其他任何地方都没有得到很好的解释.
Chr*_*ers 44
我知道这是一个相对陈旧的问题,但我发现这取决于你正在运行什么样的测试.我也在运行Rails 4和RSpec 3.2,所以我确信有些事情已经改变,因为这个问题被问到了.
请求规格
before { host! "#{mock_subdomain}.example.com" }
Run Code Online (Sandbox Code Playgroud)
Capybara的特征规格
before { Capybara.default_host = "http://#{mock_subdomain}.example.com" }
after { Capybara.default_host = "http://www.example.com" }
Run Code Online (Sandbox Code Playgroud)
我通常创建模块spec/support,看起来像这样:
# spec/support/feature_subdomain_helpers.rb
module FeatureSubdomainHelpers
# Sets Capybara to use a given subdomain.
def within_subdomain(subdomain)
before { Capybara.default_host = "http://#{subdomain}.example.com" }
after { Capybara.default_host = "http://www.example.com" }
yield
end
end
# spec/support/request_subdomain_helpers.rb
module RequestSubdomainHelpers
# Sets host to use a given subdomain.
def within_subdomain(subdomain)
before { host! "#{subdomain}.example.com" }
after { host! "www.example.com" }
yield
end
end
Run Code Online (Sandbox Code Playgroud)
包括在spec/rails_helper.rb:
RSpec.configure do |config|
# ...
# Extensions
config.extend FeatureSubdomainHelpers, type: :feature
config.extend RequestSubdomainHelpers, type: :request
end
Run Code Online (Sandbox Code Playgroud)
然后你可以在你的规范中调用如下:
feature 'Admin signs in' do
given!(:admin) { FactoryGirl.create(:user, :admin) }
within_subdomain :admin do
scenario 'with valid credentials' do
# ...
end
scenario 'with invalid password' do
# ...
end
end
end
Run Code Online (Sandbox Code Playgroud)
小智 8
在rails 3中,我试图手动设置主机的一切都不起作用,但查看代码我注意到他们解析了你传递给请求助手的路径是多么好get.果然如果你的控制器去了并提取子域中提到的用户并将其存储为@king_of_the_castle
it "fetches the user of the subomain" do
get "http://#{mock_subdomain}.example.com/rest_of_the_path"
assigns[:king_of_the_castle].should eql(User.find_by_name mock_subdomain)
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18672 次 |
| 最近记录: |