所以我认为 javascript 方法看起来像这样:
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();
天气是我的应用程序中的控制器方法。我将如何处理响应,所以当我运行测试时,它可以工作?这是我的测试的样子:
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
这是视图的简化版本/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" %>
我究竟做错了什么?我的测试失败了,但它在浏览器中运行良好。