针对Capybara JavaScript测试的XHR请求存根

时间:2011-08-11 10:39:21

标签: ruby-on-rails jquery rspec capybara jquery-autocomplete

我正在尝试编写一个request_spec来测试页面的功能,其中包含执行ajax自动完成查找的表单并返回有效结果,然后激活提交按钮。

我想知道是否有人有相同的问题,并找到了一些或参考来让这个测试通过。

的Gemfile

gem 'rake', '0.8.7'
gem 'rails', '2.3.5'
group :test do
  gem "capybara", :git => "git://github.com/jnicklas/capybara.git"
  gem 'rspec', '1.3.1'
  gem 'rspec-core'
  gem 'rspec-rails', '1.3.2'
  gem 'webmock', '1.4.0'
end

HTML

    ...
    <fieldset>
      <label>Enter a Destination</label>
      <%= text_field_tag :reviewable_name, nil, :id => "destination_autocomplete", :class => 'textfield'  %>
      <ul>
        <li>
          <h3 class="disabled">
            <%= submit_tag "Read reviews", :id => "read_a_destination_review", :name => "agree", :class => "read_review", :disabled => false %>
          </h3>
        </li>
        <li class="or">or</li>
        <li>
          <h3>
            <%= submit_tag "Write review", :id => "write_a_destination_review", :name => "agree", :class => "write_review", :disabled => false %>
          </h3>
        </li>
      </ul>
    </fieldset>
    ...

JavaScript(jQuery 1.4.4)

  ...
  $("#hotel_autocomplete, #destination_autocomplete").autocomplete({
    source: function(request, response){
      $.getJSON('/ajax/search_reviewable?type=destination&q=' + request.term,
        function(results, status){
          if(status == 'success'){
            //make it regex safe
            var term = request.term.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi, "\\$1");
            term = term.split(' ').join('|');
            for(var i = 0; i < results.length; i++){
              results[i].value = results[i].label;
              results[i].label = results[i].value.replace(new RegExp("("+term+")", "gi"),'<strong>$1</strong>');
            }
            response(results);
          }
        }
      );
    },
    minLength: 3,
    delay: 500,
    select: function( event, ui ) {...}
  });
  ...

JSON响应

/ AJAX / search_reviewable类型=目的地&安培; Q =佛罗里达

[{"label":"Florida","id":124}]

RSpec测试

    it "should activate submit button", :js => true do
      stub_request(:get, "/ajax/search_reviewable/type=destination&q=florida").to_return(:body => %Q{[{"label":"Florida","id":124}]}, :headers => {"Content-Type" => "applicaation/json; charset=utf-8", "Accept" => "*/*"})
      fill_in("destination_autocomplete", :with => "florida")
      page.execute_script(%Q{$("#destination_autocomplete").trigger("keyup");})
      find(".ui-menu-item a").click
      # should post to '/review/new', :id => 124
      page.should have_content("Write a Destination Review")
    end

测试失败,因为此规范无法在JavaScript中存根XHR请求。

感谢您的意见回复和帮助。

1 个答案:

答案 0 :(得分:0)

我设法找到解决此问题的方法。

实际发生了什么,AJAX请求页面/ajax/search_reviewable?type=destination&q=$1正在向另一个服务执行Net :: HTTP请求。

此请求需要与我需要的响应一起存根。

stub_request(:get, ".../all_by_partial_name/Florida/").to_return({
  :body => %Q{[{"label":"Florida","id":124}]}, 
  :headers => {"Content-Type" => "applicaation/json; charset=utf-8", "Accept" => "*/*"}
})

经过一些重大研究后,我发现你必须在JavaScript中触发事件才能通过测试。

源。 Clicking AutoComplete Options Gist

现在,规范示例如下所示

before(:each) do
  stub_request(:get, ".../all_by_partial_name/Florida/").to_return({
    :body => %Q{[{"label":"Florida","id":124}]}, 
    :headers => {"Content-Type" => "applicaation/json; charset=utf-8", "Accept" => "*/*"}
  })
end

it "should activate submit button and direct to new review", :js => true do
  fill_in("destination_autocomplete", :with => "florida")
  sleep 2
  page.execute_script(%Q{$(".ui-menu-item a:eq(0)").trigger("mouseenter").click();})
  find_button("write_a_destination_review").click
  page.should have_content("Write a Destination Review")
end

我希望这可以帮助您解决RSpec JQuery问题。