0
私のアプリのためにいくつかの単体テストに書き込みたいと思っています。
下のマイサービスコード:以下
function CurrenciesService($http, $q)
{
return {
getCurrencies: function()
{
var rates = [];
var urls = [{url: 'https://api.nbp.pl/api/exchangerates/rates/c/usd/today/'}, {url: 'https://api.nbp.pl/api/exchangerates/rates/c/eur/today/'},
{url: 'https://api.nbp.pl/api/exchangerates/rates/c/gbp/today/'}];
var urlsCalls = [];
angular.forEach(urls, function (url)
{
urlsCalls.push($http.get(url.url));
});
return $q.all(urlsCalls)
.then(function (result)
{
angular.forEach(result, function (rate)
{
var currency = {};
currency.code = rate.data.code;
currency.sell = rate.data.rates[0].bid;
currency.buy = rate.data.rates[0].ask;
currency.date = rate.data.rates[0].effectiveDate;
rates.push(currency);
});
return rates;
});
}
};
}
と私のユニットテスト:私は、テストユニットを決してしない
Error: Unexpected request: GET https://api.nbp.pl/api/exchangerates/rates/c/eur/today/
No more request expected in app/bower_components/angular-mocks/angular-mocks.js (line 1421)
[email protected]/bower_components/angular-mocks/angular-mocks.js:1421:90
[email protected]/bower_components/angular/angular.js:11947:21
[email protected]/bower_components/angular/angular.js:11742:23
[email protected]/bower_components/angular/angular.js:16606:30
app/bower_components/angular/angular.js:16622:39
[email protected]/bower_components/angular/angular.js:17913:28
[email protected]/bower_components/angular/angular.js:17727:36
[email protected]/bower_components/angular-mocks/angular-mocks.js:1813:45
test/unit/services/currenciesService.spec.js:54:34
:
describe('CurrenciesService', function()
{
'use strict';
var CurrenciesMock;
var httpMock;
var qMock;
var httpBackend;
var fakeRates = [];
var urls;
var urlsCalls = [];
beforeEach(module('cinkciarzTraining'));
beforeEach(inject(function (_$httpBackend_,_CurrenciesService_, _$http_, _$q_)
{
httpBackend = _$httpBackend_;
qMock = _$q_;
httpMock =_$http_;
CurrenciesMock = _CurrenciesService_;
urls = [{url: 'https://api.nbp.pl/api/exchangerates/rates/c/usd/today/'}, {url: 'https://api.nbp.pl/api/exchangerates/rates/c/eur/today/'},
{url: 'https://api.nbp.pl/api/exchangerates/rates/c/gbp/today/'}];
angular.forEach(urls, function (url)
{
urlsCalls.push(httpMock.get(url.url));
});
fakeRates = [
{
buy: 4.1635, code: 'USD', date: '2017-01-20', sell: 4.0811
}, {
buy: 4.4112, code: 'EUR', date: '2017-01-20', sell: 4.3238
}, {
buy: 5.123, code: 'GBP', date: '2017-01-20', sell: 5.0216
}
];
}));
describe('getCurencies', function()
{
describe('when call requests', function()
{
beforeEach(function()
{
httpBackend.whenGET(urls[0].url).respond(fakeRates[0]);
});
it('should return rates', function()
{
CurrenciesMock.getCurrencies().then(function(result){
expect(result).toEqual(fakeRates);
});
httpBackend.flush();
});
});
});
});
私がテストを実行すると、私はエラーを得ました$ httpと私はどのようにこのテストを書くのか分からない。誰かが私が何をしているのかを説明することができますか?
私は 'httpBackend.flush(count)'を実行しますが、これは機能しませんが、同じエラーが表示されます。 –
(i = 0; i
同じです。しかし、beforeEachでは最初のurlだけを呼び出すので、httpBackend.flush()を何度も使うべきですか? –