[英]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.