【问题标题】:Best way to debug rspec integration tests with capybara?使用 capybara 调试 rspec 集成测试的最佳方法?
【发布时间】:2012-07-24 21:23:01
【问题描述】:

我的 rspec 测试失败,但在开发中,当我自己执行操作时,页面会正确呈现。

这是sn-p:

describe "sign up process" do

let (:admin1) { FactoryGirl.create(:admin) }

describe "with valid information"

  before do 
    visit new_admin_registration_path
    fill_in "Email", with: admin1.email
    fill_in "Password", with: admin1.password
    fill_in "Password confirmation", with: admin1.password_confirmation
    click_button "Sign up"
  end

  it { should have_selector('div.alert') }
end

根本没有找到那个选择器,但是我在使用开发环境查看源代码时确认它存在。

我想做的是检查正在呈现的页面,看看我缺少什么。有没有办法做到这一点?

【问题讨论】:

    标签: ruby-on-rails-3 rspec-rails


    【解决方案1】:

    Capybara 有一个方法save_and_open_page,它将打开一个带有当前页面状态的新浏览器窗口(它需要launchy gem)。

    例如:

      before do 
        ...
        click_button "Sign up"
        save_and_open_page
      end
    

    【讨论】:

    • 这是一个非常有用的方法。谢谢!
    【解决方案2】:

    这就像在你的规范中使用调试器或其他不同的东西一样简单。

    我个人更喜欢 pry,因为每次我用 ruby​​ 1.9 尝试安装调试器时都会有点麻烦...

    使用 pry 你只需在你的 spec_helper.rb 中添加一个require "pry",然后在问题发生之前写一个binding.pry。然后你有一个 irb 会话附加到你的测试。

    【讨论】:

    • 对于调试测试非常有用。如果您正在使用 cucumber 进行集成测试,您应该在 /features/support/env.rb 文件中使用 require 'pry' 而不是 spec_helper.rb
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多