【问题标题】:Rspec/Capybara`js: true` issueRspec/Capybara`js: true` 问题
【发布时间】:2016-02-10 19:47:48
【问题描述】:

我在我的 Rails 应用程序中使用 Rspec/Capybara。在一项功能测试中,我有js: true 选项来检查模式并单击其中的按钮。在我为多租户和 PosgreSQL 模式添加 apartment gem 之前工作正常。

现在它没有找到调用模态的按钮。我不确定它是否已正确记录(binding.pry 无法在 js 的测试中正确工作)。 我认为这是 DatabaseCleaner 问题,但我的配置似乎是正确的(见下文)。

RSpec.configure do |config|
  config.use_transactional_fixtures = false

  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
 end

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end

  config.before(:each, js: true) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:each) do
    Apartment::Tenant.switch!
    DatabaseCleaner.start
  end

  config.append_after(:each) do
    DatabaseCleaner.clean
    Apartment::Tenant.reset
  end
end

可以建议哪些选项来解决此问题?

【问题讨论】:

    标签: ruby-on-rails ruby rspec capybara multi-tenant


    【解决方案1】:

    HereApartment 的 RSpec 配置示例。请注意架构被丢弃的部分。

    RSpec.configure do |config|
      config.before(:suite) do
        # Clean all tables to start
        DatabaseCleaner.clean_with :truncation
        # Use transactions for tests
        DatabaseCleaner.strategy = :transaction
        # Truncating doesn't drop schemas, ensure we're clean here, app *may not* exist
        Apartment::Tenant.drop('app') rescue nil
        # Create the default tenant for our tests
        Company.create!(name: 'Influitive Corp.', subdomain: 'app')
      end
    
      config.before(:each) do
        # Start transaction for this test
        DatabaseCleaner.start
        # Switch into the default tenant
        Apartment::Tenant.switch! 'app'
      end
    
      config.after(:each) do
        # Reset tentant back to `public`
        Apartment::Tenant.reset
        # Rollback transaction
        DatabaseCleaner.clean
      end
    end
    

    您还可以将DatabaseCleaner 配置的一部分重写为更短一些

    config.before(:each) do |example|
      DatabaseCleaner.strategy = example.metadata[:js] ? :truncation : :transaction
      DatabaseCleaner.start
      ...
    end
    

    【讨论】:

    • 感谢这些改进的设置。虽然,我仍然无法通过测试。我什至编写了更简单的测试来检查是否存在或文本“帮助”(在每个页面上,无论用户是否登录),它也失败了。似乎在feature "AdminSpec", type: :feature, js: true do 页面没有按应有的方式呈现。
    • 好吧,继续调试渲染的内容。也许您会收到有用的错误消息。
    • 我添加了save_and_open_page 命令,它显示水豚似乎什么都没有,我得到完全空白的页面,没有错误等。它是自定义的,应用程序范围的页面/help。当我从我的功能中删除 js: true 时,页面会按原样呈现
    • 你把页面的全部内容(如html代码)都转储了吗?
    • 是的。其实我找到了原因,现在将添加答案。
    【解决方案2】:

    找出js: true 问题的真正原因。 问题是我的“capybara.rb”中有config.block_unknown_url。 为了测试多租户,我使用lvh.me 域,所以我需要将config.allow_url("*.lvh.me") 添加到“capybara.rb”文件以修复损坏的js: true 东西。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-07
      • 1970-01-01
      • 2012-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-17
      相关资源
      最近更新 更多