나는 적어도 8 시간 동안 검색하고 땜질하고 비어있는 손을 올리면 커뮤니티의 도움이 필요하다는 쟁점이 생겼다. 유사한 문제를 다루려고하는 기사가 있지만 예제가 부족하고 약간의 도움을 제공하지만 완전히 아닙니다. 읽은 후에 ngWeather 팩토리의 $ 백엔드를 계산할 수 없었던 다음 모듈과 테스트를 제안합니다. $ backend .flush()를 시도 할 때마다 보류중인 요청이 없다는 오류가 발생합니다. 이것이 문제가되는 동안 JSONP 요청에 전달 된 URL이 올바르게 조롱되었는지 확신 할 수 없습니다. 모든 지침이나 방향은 크게 감사하겠습니다.
/* 모듈 및 컨트롤러 JS */
var app = angular.module('test', []);
app.controller('MainCtrl', function ($scope, ngWeather) {
ngWeather.getWeather(139,35).then(function(data) {
$scope.data = data.data;
});
});
app.factory('ngWeather', function ($http) {
return {
getWeather : function (lat, lon, callback) {
$http.jsonp('http://api.openweathermap.org/data/2.5/weather?lat=' + lat + '&lon=' + lon + '&callback=JSON_CALLBACK')
.then(function(data) {
callback({
data : data.data,
city : data.data.name,
temp : Math.floor(data.data.main.temp*(9/5)-459.67),
minTemp : Math.floor(data.data.main.temp_min*(9/5)-459.67),
maxTemp : Math.floor(data.data.main.temp_max*(9/5)-459.67),
humidity : data.data.main.humidity,
currentCondition : data.data.weather[0].main,
currentDescription : data.data.weather[0].description,
icon : data.data.weather[0].icon
});
});
}
};
});
/* 테스트 */
describe('Test: ngWeather', function() {
var ngWeather = jasmine.createSpyObj('ngWeather', ['getWeather']);
var $httpBackend;
beforeEach(module('test'));
beforeEach(inject(function(_$httpBackend_) {
$httpBackend = _$httpBackend_;
$httpBackend.when('jsonp', '*/api.openweathermap.org/*')
.respond({
data : {"coord":{"lon":0,"lat":0},"sys":{"message":0.0472,"country":"US","sunrise":1383998825,"sunset":1384035744},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"base":"gdps stations","main":{"temp":289.68,"humidity":35,"pressure":958,"temp_min":276.48,"temp_max":301.48},"wind":{"speed":4.11,"gust":5.65,"deg":169},"clouds":{"all":92},"dt":1384022616,"id":4291884,"name":"Flatwoods","cod":200},
city : 'Flatwoods',
temp : 63,
minTemp : 55,
maxTemp : 68,
humidity : 70,
currentCondition : 'Clouds',
currentDescription : 'Overcast clouds',
icon : '04n'
});
}));
it('should check if getWeather method is defined', function() {
expect(ngWeather.getWeather).toBeDefined();
});
it('should check if a value is returned', function() {
ngWeather.getWeather(139,35,function(data) {
expect(data).not.toBe(null);
});
});
it('should check if a Flatwoods is returned as city', function() {
ngWeather.getWeather(139,35, function(data) {
expect(data.city).toEqual('Flatwoods');
});
});
it('should make a call to the api', function() {
$httpBackend.expect('jsonp', '*/api.openweathermap.org/*')
.respond({
data : {"coord":{"lon":0,"lat":0},"sys":{"message":0.0472,"country":"US","sunrise":1383998825,"sunset":1384035744},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"base":"gdps stations","main":{"temp":289.68,"humidity":35,"pressure":958,"temp_min":276.48,"temp_max":301.48},"wind":{"speed":4.11,"gust":5.65,"deg":169},"clouds":{"all":92},"dt":1384022616,"id":4291884,"name":"Flatwoods","cod":200},
city : 'Flatwoods',
temp : 63,
minTemp : 55,
maxTemp : 68,
humidity : 70,
currentCondition : 'Clouds',
currentDescription : 'Overcast clouds',
icon : '04n'
});
$httpBackend.flush();
ngWeather.getWeather(139,35, function(data) {
expect(data.temp).toEqual('63');
});
});
});
이 쓰기 테스트에서 처음으로 이동하지만, 내가 좋아하는 것 품질 향상을 위해 필자는 배움이 중요하다는 것을 느낀다. 또 어떤 도움이나 방향은 크게 Here is a link to the plunker
아하, 고마워. 이것이 첫 번째 시험 인 곳에서는 조롱하고 구현하는 방법을 이해하는 데 익숙한 시각 자료가 정말 필요했습니다. 너 그거 줬어. 귀하의 도움을 크게 주시면 감사하겠습니다. –