【问题标题】:Starting rails before rspec tests?在 rspec 测试之前启动 rails?
【发布时间】:2011-11-27 23:56:11
【问题描述】:

在 RSpec 测试中,按照约定或代码,是否有任何方法可以在测试运行之前启动 rails?我正在尝试为使用 chrome 的 selenium 测试设置一个测试框架,现在我只是因为缺少正在运行的服务器而受阻。

require 'spec_helper'

describe 'The first tab' do
  before(:each) do
    @driver = Selenium::WebDriver.for :chrome
  end

  it 'Shows the list' do
    @driver.navigate.to 'index.html'
  end
end

我是 RSpec 的新手,所以我不确定如何创建一套在 Rails 服务器运行时全部运行的测试。

【问题讨论】:

    标签: ruby-on-rails ruby selenium rspec selenium-webdriver


    【解决方案1】:

    您应该改用Capybara 来测试这些东西。它在内部使用 selenium-webdriver 来提供 JavaScript 测试支持。

    使用 Capybara,您可以将此测试放在 spec/integrationspec/requests 文件夹中,然后这样写:

    require 'spec_helper'
    describe 'The first tab' do
      it "shows the list", :js => true do
        visit list_path
      end
    end
    

    通过在示例名称后面加上:js => true,Capybara 会知道将其作为 JavaScript 测试运行。

    【讨论】:

    • 直接使用webdriver的文档很好;缺少水豚。起初我使用它,并意识到我不需要 capybara,只要我可以启动一个服务器来运行测试。
    • +1 来自我。 -1 不应该是针对不同意的答案,而是针对不相关/无帮助的答案,我认为这里不是这种情况。
    • 另外,请查看spork,它只会初始化您的 Rails 环境一次。
    • 另外,使用 Cucumber,而不是 RSpec 来测试你面向用户的东西。
    • @MarnenLaibow-Koser:Cucumber 非常适合运行和管理由原始驱动程序驱动的硒测试。它确实比 RSpec 本身运行得更好更快,并且测试更清晰,更具描述性。喜欢它!
    【解决方案2】:

    只需运行 rails server 并终止进程即可完成测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多