【问题标题】:Integration Testing with: Best in Place, Capybara & RSpec集成测试:Best in Place、Capybara 和 RSpec
【发布时间】:2014-09-29 12:35:20
【问题描述】:

如何使用 Capybara 和 Rspec 测试这样的 best_in_place 字段?我是新手。

<span 
   class='best_in_place' 
   id='best_in_place_user_233952_name' 
   data-url='/users/233952' 
   data-object='user' 
   data-attribute='name' 
   data-nil='User Name' 
   data-type='textarea' 
   data-original-content=''>
</span> 

我想用名称填写最佳位置文本区域。

【问题讨论】:

标签: ruby-on-rails rspec capybara best-in-place


【解决方案1】:

我就是这样解决的:

我必须将config.include BestInPlace::TestHelpers 添加到我的spec_helper.rb 才能使用best in place test helpers

RSpec.configure do |config|  
  config.include BestInPlace::TestHelpers 
end

在我的测试中,我这样做了:

require 'spec_helper'

feature 'create a new user' do 
    scenario "create user Alice", :js => true do
        visit '/some_path'

        click_on 'New User'
        expect(page).to have_content 'User Name' # [1]

        user = User.order(:created_at).last
        bip_area user, :name, 'Alice'

        expect(page).to have_content 'Alice'
    end  
end

[1] 很重要,因为它会等到 JS 加载完字段!

【讨论】:

  • 感谢代码示例。对于其他阅读本文的人,如果您还没有这样做,您可能还需要将 require 'best_in_place'require 'best_in_place/test_helpers' 添加到您的 spec_helper.rb 文件中
  • 对我来说,需要添加的是这两行:require 'best_in_place/test_helpers'include BestInPlace::TestHelpers
【解决方案2】:

假设您启用了 JavaScript:

fill_in("best_in_place_user_#{user.id}_name", with: 'some text')
page.execute_script "$('#best_in_place_user_#{user.id}_name').trigger('keyup')"

【讨论】:

  • 好的,但是我如何在这里获得正确的 id?因为233952是动态生成的。
  • 您将 id 插入到字符串中。示例代码已更新以显示这一点。
  • "best_in_place_user_#{user.id}_name" 返回正确的 id,谢谢!但是如果我在fill_in 中使用它,测试会返回这个错误:Capybara::ElementNotFound: Unable to find field "best_in_place_user_2_name"user.id=2 是正确的,我已经检查过了。还有:js=&gt;true)。
  • 你使用的是javascript驱动吗?首先需要 Selenium、phantomjs 或类似的东西才能使 js 运行。如果是这样,那么您有错误元素的 id。最好的地方添加一个输入来输入,所以你需要为此获取 id。上面那个是span元素不能填的。
猜你喜欢
  • 1970-01-01
  • 2012-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多