كتابة اختبار لتوجيه يقوم بإعادة تحميل صفحة كاملة في الكرمة
-
21-12-2019 - |
سؤال
وأود أن وحدة اختبار التوجيه الذي يعيد توجيه المستخدم إلى عنوان ورل تسجيل الدخول الاجتماعي في المدعومة.
نظرا لأن الكارما لا تدعم إعادة تحميل الصفحة بالكامل ، أود تغيير سلوك الموقع.كائن هريف جافا سكريبت لإخراج المعلمة التي يتلقاها إلى عنصر هتمل مع معرف معين ، وأنا أواجه صعوبات للقيام بذلك.
التوجيه:
__app.directive('socialAuth', function(utils, authService, $location){
return{
restrict: 'A',
scope: false,
link: function(scope, elem, attrs){
elem.bind('click', function(){
utils.cleanSocialSearch();
if(attrs.checkbox == 'personal'){
scope.$apply(function(){
scope.model.personalShare[attrs.network] = true;
$location.search('personalShare', '1');
});
}
else if(attrs.checkbox == 'group'){
scope.$apply(function(){
var __index = attrs.checkbox + '_' + attrs.network;
scope.model.personalShare[__index] = true;
$location.search('groupShare', '1');
});
}
var callback = encodeURIComponent(window.location.href);
var loginUrl = utils.getBaseUrl() + '/social/login/' + attrs.network + '?success_url=' + callback;
location.href = loginUrl;
});
}
}
});
اختبار مع محاولة للسخرية من الموقع.كائن هريف (نعم ، وأنا أعلم أنها ليست وظيفة):
var location = {//an attempt to mock the location href object
href: function(param){
$('#content').html(param);
}
};
'use strict';
describe('socail-auth', function(){//FB
var scope, compiled, linkFn, html, elem, elemPersonal, elemGroups, compile, authService;
html = "<span id='content' data-social-auth data-network='facebook'>";
beforeEach(function(){
module('myApp.directives');
module('myApp.services');
inject(function($compile, $rootScope){
scope = $rootScope.$new();
linkFn = $compile(angular.element(html));
elem = linkFn(scope);
scope.$digest();
elem.scope().$apply()
});
})
it("should redirect user to social login url at the backend", function(){
// console.log(location.href);
elem.click();
console.log($(elem).html());
expect($(elem).html()).toBeDefined();
});
});
المحلول 2
الحل هو التفاف إعادة التوجيه إلى API في وظيفة الخدمة، وسماصله ضمن الاختبار لحفظ عنوان URL إعادة توجيه في متغير الخدمة، ثم تعرضه بعد طريقة Getter إلى الاختبار بدلا من إعادة توجيه إلىذلك.
يمكن عملها من داخل الجسم الاختبار:
giveacodicetagpre.لكنني اخترت فصل هذا المزود بموجب هذه النموذج الخاص به من أجل تعزيز الهيكل والطيوفية
giveacodicetagpre.-
giveacodicetagpre.وأخيرا الاختبار:
giveacodicetagpre.نصائح أخرى
استخدام $window.location.href
بدلا من location.href
.
ثم وهمية $window.location
مع كائن فارغ ، فإنه سيتم القيام بهذه المهمة.
describe('location', function() {
var $window;
beforeEach( module('myApp') );
// You can copy/past this beforeEach
beforeEach( module( function($provide) {
$window = {
// now, $window.location.path will update that empty object
location: {},
// we keep the reference to window.document
document: window.document
};
// We register our new $window instead of the old
$provide.constant( '$window' , $window );
}))
// whatever you want to test
it('should be redirected', function() {
// action which reload the page
$scope.reloadPage();
// we can test if the new path is correct.
expect( $window.location.path ).toBe('/the-url-expected');
})
})