Rails rspec 设置子域

发布于 2024-08-28 01:02:53 字数 295 浏览 9 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(4

旧竹 2024-09-04 01:02:53

我想出了如何解决这个问题。

在我的规范中的 before 块中,我简单地添加了:

before(:each) do
  @request.host = "#{mock_subdomain}.example.com"
end

这将 request.subdomains.first 设置为 mock_subdomain 的值。

希望有人发现这很有用,因为它在网络上的其他地方没有得到很好的解释。

I figured out how to sort this issue.

In my before block in my specs I simply added:

before(:each) do
  @request.host = "#{mock_subdomain}.example.com"
end

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.

若水微香 2024-09-04 01:02:53

我知道这是一个相对较老的问题,但我发现这取决于您正在运行的测试类型。我还运行 Rails 4 和 RSpec 3.2,所以我确信自从提出这个问题以来,有些事情已经发生了变化。

请求规格

before { host! "#{mock_subdomain}.example.com" }

Capybara 的功能规格

before { Capybara.default_host = "http://#{mock_subdomain}.example.com" }
after  { Capybara.default_host = "http://www.example.com" }

我通常在 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

包含在 spec/rails_helper 中。 rb

RSpec.configure do |config|
  # ...

  # Extensions
  config.extend FeatureSubdomainHelpers, type: :feature
  config.extend RequestSubdomainHelpers, type: :request
end

然后你可以在你的规范中调用,如下所示:

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

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

before { host! "#{mock_subdomain}.example.com" }

Feature Specs with Capybara

before { Capybara.default_host = "http://#{mock_subdomain}.example.com" }
after  { Capybara.default_host = "http://www.example.com" }

I usually create modules in spec/support that look something like this:

# 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

Include in spec/rails_helper.rb:

RSpec.configure do |config|
  # ...

  # Extensions
  config.extend FeatureSubdomainHelpers, type: :feature
  config.extend RequestSubdomainHelpers, type: :request
end

Then you can call within your spec like so:

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
维持三分热 2024-09-04 01:02:53

在 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

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

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
玩世 2024-09-04 01:02:53
  • Rspec - 3.6.0
  • Capybara - 2.15.1

Chris Peters 的答案对我的请求规范有用,但对于功能规范,我必须进行以下更改:

rails_helper:

Capybara.app_host = 'http://lvh.me'
Capybara.always_include_port = true

feature_subdomain_helpers:

module FeatureSubdomainHelpers
    def within_subdomain(subdomain)
        before { Capybara.app_host = "http://#{subdomain}.lvh.me" }
        after  { Capybara.app_host = "http://lvh.me" }
        yield
    end
end
  • Rspec - 3.6.0
  • Capybara - 2.15.1

Chris Peters' answer worked for me for Request specs, but for Feature specs, I had to make the following changes:

rails_helper:

Capybara.app_host = 'http://lvh.me'
Capybara.always_include_port = true

feature_subdomain_helpers:

module FeatureSubdomainHelpers
    def within_subdomain(subdomain)
        before { Capybara.app_host = "http://#{subdomain}.lvh.me" }
        after  { Capybara.app_host = "http://lvh.me" }
        yield
    end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文