0

したがって、ビューには次のような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();

weather は、私のアプリケーションのコントローラー メソッドです。テストを実行したときに応答をスタブ化するにはどうすればよいですか? 私のテストは次のようになります。

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" %>

私は何を間違っていますか?私のテストは失敗しましたが、ブラウザでは問題なく動作しています。

4

1 に答える 1

1

visit '/dashboard/RSF/pv'HTMLリクエストを実行している場合、JSONリクエストを実行したい場合は、代わりにこれを使用してみてください。

    get '/your/path', format: 'js'
于 2013-03-20T22:46:18.553 に答える