Bav*_*sjo 7 rspec routes ruby-on-rails
Url帮助程序(例如root_url)在app控制器和rspec示例中返回不同的主机名.我已成功地在我的rails应用程序中为url helpers设置域,如下所示:
class ApplicationController < ActionController::Base
def default_url_options
{host: "foo.com", only_path: false}
end
end
Run Code Online (Sandbox Code Playgroud)
例如,root_url在应用程序中输出"http://foo.com/",但在我的请求规范中输出"http://example.com".在rails 3.2中全局应用这些url选项的建议方法是什么,以便它们影响我的规范?网址选项不需要是动态的.
只是为了添加 Dan 的答案,它在spec_helper.rb 中看起来像这样
RSpec.configure do |config|
# other config options
config.before(:each) do
host! "localhost:3000"
end
end
Run Code Online (Sandbox Code Playgroud)
在功能规格中,主持人!已被弃用.将这些添加到您的rspec_helper.rb:
# Configure Capybara expected host
Capybara.app_host = 'http://lvh.me:3000'
# Configure actual routes host during test
before(:each) do
if respond_to?(:default_url_options)
default_url_options[:host] = 'http://lvh.me:3000'
end
end
Run Code Online (Sandbox Code Playgroud)