简体   繁体   中英

RSpec, Capybara and an ajax request with rails

So I have javascript method in my view that looks like this:

window.getWeatherData = function () {
$.getJSON('/weather.json?building=RSF', function (response) {
  console.log(response)
  $('#dynamic-wrapper').show();
  $('#weather-meridan').html(response.meridan);
  $('#relative-humidity').html(response.rh);
  $('#outside-temp').html(response.temperature);
  $('#windspeed').html(response.windspeed);
  // TODO still need to get wind direction!
}).error(function () {
  $('#dynamic-wrapper').hide();
});
}

getWeatherData();

weather is a controller method in my application. How would I go about stubbing the response so when I run my test, it works? Here's what my tests look like:

before :each do
  MeterMapsController.any_instance.stub(:weather).and_return(
    :json => {:temperature => 98.6, :rh => 100, :windspeed => 20}
  )
end

it 'shows relative humidity' do
  visit '/dashboard/RSF/pv'
  find('span#relative-humidity').should have_content '100% Outside Relative Humidity'
end

Here's a simplified version of the view that's at /dashboard/RSF/pv

<div class='current-data'>
<span id='weather-time' class='digi'></span><span id='weather-meridan'></span>
  <span id="dynamic-wrapper">
    <span id='relative-humidity' class='digi'></span>% Outside Relative Humidity <br />
    <span id='outside-temp' class='digi'></span>ºF
    <span id='windspeed' class='digi'></span> mph Wind Speed<%# out of %>
    <span id='wind-direction'></span>
  </span>
</div>
<%= javascript_include_tag "weather.js" %>

What am I doing wrong? My test fails, but it's working just fine in the browser.

visit '/dashboard/RSF/pv'正在执行HTML请求,您想执行JSON请求,请尝试使用以下方法:

    get '/your/path', format: 'js'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM