[英]RSpec, Capybara and an ajax request with rails
因此,我认为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" %>
我究竟做错了什么? 我的测试失败,但在浏览器中工作正常。
visit '/dashboard/RSF/pv'
正在执行HTML请求,您想执行JSON请求,请尝试使用以下方法:
get '/your/path', format: 'js'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.