1
投稿機能が私のジャスミンテストに合格しない理由を理解できません。私のジャスミンでポストHTTP機能を使って私のテストに合格する
.controller('SystemControllerNg',
['sweetAlertService', 'dataExchangeService', function(sweetAlertService, dataExchangeService) {
var self = this;
self.all_products = [];
/*
Get product from database
*/
self.getProductsFromDatabase = function() {
var success = function(response) {
self.all_products = response.data;
};
var error = function(errResponse) {
console.log('Error getting products from database');
};
dataExchangeService.getProducts().then(success, error);
};
/*
End get product
*/
/*
Send product to database
*/
self.sendProductToDatabase = function(type) {
var success = function(response) {
self.getProductsFromDatabase();
};
var error = function(errResponse) {
console.log('Error sending product to database');
};
dataExchangeService.sendProduct({
"owner": 1,
"name": self.product_input,
"type": true,
"size": 0,
//"assign_category": self.categoryName_id
"assign_category": null
}).then(success, error);
};
/*
End send product
*/
/*
Functions RUNNING in the background
*/
self.getProductsFromDatabase();
/*
End functions RUNNING in the background
*/
}])
.factory('dataExchangeService', ['$http', function($http) {
return {
getProducts: function() {
return $http.get('/api/v1/Products/');
},
sendProduct: function(dataObject) {
return $http.post('/api/v1/Products/', dataObject);
}
};
}]);
とテストコード:
describe('Testing services in "Aplication" module', function() {
beforeEach(module('Aplication'));
describe('SystemControllerNg controller', function() {
var ctrl, mockBackend;
beforeEach(inject(function($controller, $httpBackend) {
mockBackend = $httpBackend;
ctrl = $controller('SystemControllerNg');
}));
//In this issue everything is ok
it('it should load products from database', function() {
expect(ctrl.all_products).toEqual([]);
var resObj = {
"owner": 1,
"name": "",
"type": true,
"size": 0,
"assign_category": null
};
mockBackend.expectGET('/api/v1/Products/')
.respond([ resObj ]);
mockBackend.flush();
expect(ctrl.all_products).toEqual([ resObj ]);
});
it('it should send product to database', function() {
//expect(ctrl.all_products).toEqual([]);
var dataObject = {
"owner": 1,
"name": "product",
"type": true,
"size": 0,
"assign_category": null
};
//I don't know why can't pass it :|
mockBackend.expectPOST('/api/v1/Products/', dataObject).respond(201, {});
mockBackend.flush();
//expect(ctrl.all_products).toEqual([ dataObject ]);
});
afterEach(function() {
mockBackend.verifyNoOutstandingExpectation();
mockBackend.verifyNoOutstandingRequest();
});
});
});
まずテストexpectGET仕事素晴らしいが、なぜ私は$ httpBackendの問題はこれが私のコードであるAngularJSのドキュメントで
非常に貧しいされていることを考えますexpectPOSTはありませんか?それがなぜ機能しないのか分かっているなら、私はとても感謝しています。事前に
ありがとう:
は、これは私のログコンソールです。ここ
1)ありがとうございます。でも、前もって 'ctrl.sendProductToDatabase();'を試してみましたが、まだ動作しませんでした。私はそれが最も論理的な解決策であることを知っています.. 2)あなたはおそらく正しいでしょうし、私もそれを試してみませんが、それが解決策になるなら、私にとって意味がありません: 'mockBackend.expectGET( '/ api/v1/Products/')。respond([resObj]); 'も動作しないはずですはい? –
プロセスにダイジェストサイクルを追加しようとしましたか?私の答えに加えたものに似た何か。最初にテスト設定でスコープ変数を定義する必要があります。 – MBielski
それは助けにはなりませんでしたが、私は単位検査がどのように角度で働くかを誤解していたと思いました。ご助力ありがとうございます。私は正しくここで私のソリューションを書くつもりです –