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.