【问题标题】:Capybara and Ajax test not workingCapybara 和 Ajax 测试不起作用
【发布时间】:2011-06-28 09:10:52
【问题描述】:

我使用 ajax 提交表单。当表单提交出现错误时,表单会再次使用 ajax、jquery 呈现。

我正在尝试使用 Capybara 和 Rspec 测试这种行为,如下所示:

描述“板创建失败”,:type => :request do

      it "should not create a birthday board" do
        Capybara.default_wait_time = 5
            lambda do
              visit root_path
              fill_in "Name",      :with => ""
              select('1967', :from => "board_birthday_1i" )
              select('October',   :from => "board_birthday_2i" )
              select('30',   :from => "board_birthday_3i" )
              click_button("Start the surprise")
              page.should have_selector('div#error_explanation')
              page.find_field('Name').value
            end.should_not change(Board, :count)
      end
    end

结果如下:

  Failure/Error: page.should have_selector('div#error_explanation')
       expected following output to contain a <div#error_explanation/> tag:

但实际上我在页面上有一个 error_explanation div。

为什么这个测试失败了?

提前谢谢你。

【问题讨论】:

  • 您是否在某处添加 :js => true 选项以便执行 ajax?
  • 我的 gem 文件中有 Capybara 和 webrat。我认为导致问题的 webrat 和 capybara 之间存在冲突

标签: ruby-on-rails-3 rspec2 capybara


【解决方案1】:

您是否尝试过等待几秒钟直到响应到达。

在前面添加sleep(3)

page.should have_selector('div#error_explanation')

行并再次测试。 这可能会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-13
    • 2018-06-06
    • 2013-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    相关资源
    最近更新 更多