0

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.

pizza247
  • 3,869
  • 7
  • 33
  • 47

1 Answers1

1

visit '/dashboard/RSF/pv' is doing a HTML request, you want to do a JSON request, try using this instead:

    get '/your/path', format: 'js'
Benjamin Bouchet
  • 12,971
  • 2
  • 41
  • 73
  • You can also define the route default format as JSON in the route file, if that's all it serves up. See http://stackoverflow.com/questions/10681816/render-json-instead-of-html-as-default. – Jim Stewart Mar 20 '13 at 22:48
  • Exact, I'm use to this cause often my routes have the same name for the same actions but different access. – Benjamin Bouchet Mar 20 '13 at 22:50
  • @BenjaminSinclaire this doesn't make sense. The view is html, and this method is in the view. How would this apply? I want /dashboard/RSF/pv to do an html request and test to see if the field is populated by the method. Perhaps your saying that I'm better off just testing the controller method and not the javascript population with capybara... – pizza247 Mar 21 '13 at 13:10
  • @BenjaminSinclaire I added some more info to the question. I realize it may not have been clear what I was trying to do. – pizza247 Mar 21 '13 at 14:54