Ich bin fest, den Unit Test mit Karma zu machen, ich habe keine Idee, wie der Unit Test, weil es das erste Mal ist. Ich benutze AngularJS und der Komponententest ist Karma.TypeError: Kann Eigenschaft 'firstName' von undefined Unit Test nicht lesen AngularJS
Das Ding ist das: Ich benutze einen Dienst, um den firstName, lastName und PhoneNumber des Kunden zu erhalten, um in meiner Form zu zeigen, und es funktioniert ohne irgendein Problem, ABER, wenn ich versuche, den Komponententest zu machen der Fehler ist immer:
addressForm component should load customer profile FAILED
TypeError: Cannot read property 'firstName' of undefined
addressForm.js
(function() {
'use strict';
angular
.module('shopping.address')
.component('addressForm', {
controller: AddressFormController,
controllerAs: 'addressForm',
bindings: {
input: '<',
edit: '@',
type: '@',
hideSaveButton: '@',
},
templateUrl: '/ng/address-form',
});
AddressFormController.$inject = ['event', 'customer', 'resolveLocation', 'order'];
function AddressFormController(event, customer, resolveLocation, order) {
var viewModel = this;
viewModel.$onInit = onInit;
viewModel.input = viewModel.input || {};
function onInit() {
loadCustomerProfile();
}
function loadCustomerProfile() {
viewModel.input.firstName = order.customer.firstName;
viewModel.input.lastName = order.customer.lastName;
viewModel.input.phoneNumber = order.customer.phoneNumber;
}
}
})();
Unit-Test: addressForm.spec.js:
describe('addressForm component', function() {
var component;
var scope;
beforeEach(function() {
bard.appModule('shopping.address');
bard.inject('$rootScope', '$componentController', '$q', 'resolveLocation', 'customer', 'event','order');
scope = $rootScope.$new();
component = $componentController('addressForm', { $scope: scope });
});
it('should be attached to the scope', function() {
expect(scope.addressForm).to.be.equal(component);
});
// ----------- I HAVE PROBLEMS IN THIS PART !!!!!!
it('should load customer profile', function() {
var newFirstName = { firstName: 'something'};
component.$onInit();
order.customer.firstName = { firstName: 'Something'};
order.customer.lastName = { lastName: 'Something' };
order.customer.phoneNumber = { phoneNumber: 55555555};
// component.input = {
// firstName: 'something',
// lastName: 'something',
// phoneNumber: 55555555
// };
component.loadCustomerProfile();
$rootScope.$apply();
component.input.firstName = newFirstName;
expect(component.input.firstName).to.be.equal({firstName: 'something'});
expect(component.input.lastName).to.be.not.empty;
expect(component.input.phoneNumber).to.be.null;
});
// it('should load customer last name', function() {
// component.$onInit();
// component.loadCustomerProfile().then(function() {
// expect(component.lastName).to.deep.equal({ foo: 'bar' });
// expect(component.lastName).to.be.null;
// });
//
// });
//
// it('should load customer phone number', function() {
// component.$onInit();
// component.loadCustomerProfile().then(function() {
// expect(component.phoneNumber).to.deep.equal({ foo: 'bar' });
// expect(component.phoneNumber).to.be.null;
// });
//
// });
});
Bitte lesen Sie [Unter welchen Umständen kann ich hinzufügen "Dringende" oder andere ähnliche Sätze zu meiner Frage, um schnellere Antworten zu erhalten?] (// meta.stackoverflow.com/q/326569) - Die Zusammenfassung ist, dass dies keine ideale Möglichkeit ist, Freiwillige anzusprechen, und ist wahrscheinlich kontraproduktiv, um Antworten zu erhalten. Bitte unterlassen Sie das Hinzufügen zu Ihren Fragen. – halfer
Oh, es tut mir so leid, aber ich werde im Voraus für die zukünftigen Fragen nehmen, sorry :( –