Karma TypeError caused by a typeless angular component input The Next CEO of Stack Overflow@Directive v/s @Component in AngularAngular - Use pipes in services and componentsAngular pass callback function to child component as @InputAngular2 DomSanitizer issueAngular2 NgModel not getting value in Jasmine testAngular 2 Karma Test 'component-name' is not a known elementMocking an Angular Service that has RxJS Observables for testingAngular testing with keycloack “user is not logged in”Using materialize-css (v 1.0.0) in Angular 5 does not workNgZone undefined when testing Angular component
If the heap is initialized for security, then why is the stack uninitialized?
Do I need to enable Dev Hub in my PROD Org?
What exact does MIB represent in SNMP? How is it different from OID?
What flight has the highest ratio of time difference to flight time?
Is 'diverse range' a pleonastic phrase?
Written every which way
Is there a difference between "Fahrstuhl" and "Aufzug"
What is the result of assigning to std::vector<T>::begin()?
Contours of a clandestine nature
Multiple labels for a single equation
What does convergence in distribution "in the Gromov–Hausdorff" sense mean?
If a black hole is created from light, can this black hole then move at speed of light?
Would this house-rule that treats advantage as a +1 to the roll instead (and disadvantage as -1) and allows them to stack be balanced?
Complex fractions
Are there any limitations on attacking while grappling?
How do I reset passwords on multiple websites easily?
Why don't programming languages automatically manage the synchronous/asynchronous problem?
Would a completely good Muggle be able to use a wand?
How do I go from 300 unfinished/half written blog posts, to published posts?
Interfacing a button to MCU (and PC) with 50m long cable
Why do we use the plural of movies in this phrase "We went to the movies last night."?
Rotate a column
Is it possible to search for a directory/file combination?
Is it my responsibility to learn a new technology in my own time my employer wants to implement?
Karma TypeError caused by a typeless angular component input
The Next CEO of Stack Overflow@Directive v/s @Component in AngularAngular - Use pipes in services and componentsAngular pass callback function to child component as @InputAngular2 DomSanitizer issueAngular2 NgModel not getting value in Jasmine testAngular 2 Karma Test 'component-name' is not a known elementMocking an Angular Service that has RxJS Observables for testingAngular testing with keycloack “user is not logged in”Using materialize-css (v 1.0.0) in Angular 5 does not workNgZone undefined when testing Angular component
I am receiving this error:
TypeError: Cannot read property 'url' of undefined
Cause by this section of code:
export class ImageCardComponent implements OnInit {
@Input() image: any; // Can be an image or imageToUpload
imageUrls = [];
...
ngOnInit()
this.imageUrls.push((this.image as any).url);
this.image.outOfSync = true;
test code
describe('ImageCardComponent', () =>
let component: ImageCardComponent;
let fixture: ComponentFixture<ImageCardComponent>;
beforeEach(async(() =>
TestBed.configureTestingModule(
declarations: [ ImageCardComponent ],
imports: [FormsModule],
)
.compileComponents();
));
beforeEach(() =>
fixture = TestBed.createComponent(ImageCardComponent);
component = fixture.componentInstance;
fixture.detectChanges();
component.image = getImageGetResponseMock().image;
);
it('should create', () =>
expect(component).toBeTruthy();
);
);
mock data
export function getImageGetResponseMock()
return
id: 1,
image: [
id: 2,
height: 90,
width: 160,
format: 'jpg',
url: 'https://i.imgflip.com/mlqo9.jpg?a430032',
tags: ['testTag']
]
;
How can I go about fixing this? Do I need to consolidate my two possible inputs into a common class or is there a way to provide the test script a mock of an input?
angular typescript karma-jasmine
add a comment |
I am receiving this error:
TypeError: Cannot read property 'url' of undefined
Cause by this section of code:
export class ImageCardComponent implements OnInit {
@Input() image: any; // Can be an image or imageToUpload
imageUrls = [];
...
ngOnInit()
this.imageUrls.push((this.image as any).url);
this.image.outOfSync = true;
test code
describe('ImageCardComponent', () =>
let component: ImageCardComponent;
let fixture: ComponentFixture<ImageCardComponent>;
beforeEach(async(() =>
TestBed.configureTestingModule(
declarations: [ ImageCardComponent ],
imports: [FormsModule],
)
.compileComponents();
));
beforeEach(() =>
fixture = TestBed.createComponent(ImageCardComponent);
component = fixture.componentInstance;
fixture.detectChanges();
component.image = getImageGetResponseMock().image;
);
it('should create', () =>
expect(component).toBeTruthy();
);
);
mock data
export function getImageGetResponseMock()
return
id: 1,
image: [
id: 2,
height: 90,
width: 160,
format: 'jpg',
url: 'https://i.imgflip.com/mlqo9.jpg?a430032',
tags: ['testTag']
]
;
How can I go about fixing this? Do I need to consolidate my two possible inputs into a common class or is there a way to provide the test script a mock of an input?
angular typescript karma-jasmine
add a comment |
I am receiving this error:
TypeError: Cannot read property 'url' of undefined
Cause by this section of code:
export class ImageCardComponent implements OnInit {
@Input() image: any; // Can be an image or imageToUpload
imageUrls = [];
...
ngOnInit()
this.imageUrls.push((this.image as any).url);
this.image.outOfSync = true;
test code
describe('ImageCardComponent', () =>
let component: ImageCardComponent;
let fixture: ComponentFixture<ImageCardComponent>;
beforeEach(async(() =>
TestBed.configureTestingModule(
declarations: [ ImageCardComponent ],
imports: [FormsModule],
)
.compileComponents();
));
beforeEach(() =>
fixture = TestBed.createComponent(ImageCardComponent);
component = fixture.componentInstance;
fixture.detectChanges();
component.image = getImageGetResponseMock().image;
);
it('should create', () =>
expect(component).toBeTruthy();
);
);
mock data
export function getImageGetResponseMock()
return
id: 1,
image: [
id: 2,
height: 90,
width: 160,
format: 'jpg',
url: 'https://i.imgflip.com/mlqo9.jpg?a430032',
tags: ['testTag']
]
;
How can I go about fixing this? Do I need to consolidate my two possible inputs into a common class or is there a way to provide the test script a mock of an input?
angular typescript karma-jasmine
I am receiving this error:
TypeError: Cannot read property 'url' of undefined
Cause by this section of code:
export class ImageCardComponent implements OnInit {
@Input() image: any; // Can be an image or imageToUpload
imageUrls = [];
...
ngOnInit()
this.imageUrls.push((this.image as any).url);
this.image.outOfSync = true;
test code
describe('ImageCardComponent', () =>
let component: ImageCardComponent;
let fixture: ComponentFixture<ImageCardComponent>;
beforeEach(async(() =>
TestBed.configureTestingModule(
declarations: [ ImageCardComponent ],
imports: [FormsModule],
)
.compileComponents();
));
beforeEach(() =>
fixture = TestBed.createComponent(ImageCardComponent);
component = fixture.componentInstance;
fixture.detectChanges();
component.image = getImageGetResponseMock().image;
);
it('should create', () =>
expect(component).toBeTruthy();
);
);
mock data
export function getImageGetResponseMock()
return
id: 1,
image: [
id: 2,
height: 90,
width: 160,
format: 'jpg',
url: 'https://i.imgflip.com/mlqo9.jpg?a430032',
tags: ['testTag']
]
;
How can I go about fixing this? Do I need to consolidate my two possible inputs into a common class or is there a way to provide the test script a mock of an input?
angular typescript karma-jasmine
angular typescript karma-jasmine
edited Mar 8 at 15:10
Tyler B. Joudrey
asked Mar 8 at 14:29
Tyler B. JoudreyTyler B. Joudrey
549
549
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
imageUrl
is not initialized with a value that means it will be undefined if you try to get a value from it before it has a value.
You should be mocking the objects needed in your tests and then write your test cases.
const imageUrl = image: 'testImage.png', url:'image_url';
Now After TestBed set up, have a beforeEach block to mock the needed inputs:
beforeEach(() =>
const fixture = TestBed.createComponent(ImageCardComponent );
component = fixture.componentInstance;
component.imageUrl= imageUrl;
);
it('should have image url info', () =>
expect(component.imageUrl).toBeTruthy(); // whatever you have to check for..
);
Turns out I made a typo in my question. Though I tried the general idea you are getting at to no avail
– Tyler B. Joudrey
Mar 8 at 15:05
Can you share the test code here if possible to see what's going on?
– nircraft
Mar 8 at 15:07
I added the test code, and the mock for an image
– Tyler B. Joudrey
Mar 8 at 15:10
Once you docomponent.image = getImageGetResponseMock().image;
you should runfixture.detectChanges();
to make sure your changes are available to be used
– nircraft
Mar 8 at 15:23
1
Here are few good reads: medium.com/frontend-fun/… scotch.io/tutorials/…
– nircraft
Mar 8 at 15:53
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55065265%2fkarma-typeerror-caused-by-a-typeless-angular-component-input%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
imageUrl
is not initialized with a value that means it will be undefined if you try to get a value from it before it has a value.
You should be mocking the objects needed in your tests and then write your test cases.
const imageUrl = image: 'testImage.png', url:'image_url';
Now After TestBed set up, have a beforeEach block to mock the needed inputs:
beforeEach(() =>
const fixture = TestBed.createComponent(ImageCardComponent );
component = fixture.componentInstance;
component.imageUrl= imageUrl;
);
it('should have image url info', () =>
expect(component.imageUrl).toBeTruthy(); // whatever you have to check for..
);
Turns out I made a typo in my question. Though I tried the general idea you are getting at to no avail
– Tyler B. Joudrey
Mar 8 at 15:05
Can you share the test code here if possible to see what's going on?
– nircraft
Mar 8 at 15:07
I added the test code, and the mock for an image
– Tyler B. Joudrey
Mar 8 at 15:10
Once you docomponent.image = getImageGetResponseMock().image;
you should runfixture.detectChanges();
to make sure your changes are available to be used
– nircraft
Mar 8 at 15:23
1
Here are few good reads: medium.com/frontend-fun/… scotch.io/tutorials/…
– nircraft
Mar 8 at 15:53
|
show 1 more comment
imageUrl
is not initialized with a value that means it will be undefined if you try to get a value from it before it has a value.
You should be mocking the objects needed in your tests and then write your test cases.
const imageUrl = image: 'testImage.png', url:'image_url';
Now After TestBed set up, have a beforeEach block to mock the needed inputs:
beforeEach(() =>
const fixture = TestBed.createComponent(ImageCardComponent );
component = fixture.componentInstance;
component.imageUrl= imageUrl;
);
it('should have image url info', () =>
expect(component.imageUrl).toBeTruthy(); // whatever you have to check for..
);
Turns out I made a typo in my question. Though I tried the general idea you are getting at to no avail
– Tyler B. Joudrey
Mar 8 at 15:05
Can you share the test code here if possible to see what's going on?
– nircraft
Mar 8 at 15:07
I added the test code, and the mock for an image
– Tyler B. Joudrey
Mar 8 at 15:10
Once you docomponent.image = getImageGetResponseMock().image;
you should runfixture.detectChanges();
to make sure your changes are available to be used
– nircraft
Mar 8 at 15:23
1
Here are few good reads: medium.com/frontend-fun/… scotch.io/tutorials/…
– nircraft
Mar 8 at 15:53
|
show 1 more comment
imageUrl
is not initialized with a value that means it will be undefined if you try to get a value from it before it has a value.
You should be mocking the objects needed in your tests and then write your test cases.
const imageUrl = image: 'testImage.png', url:'image_url';
Now After TestBed set up, have a beforeEach block to mock the needed inputs:
beforeEach(() =>
const fixture = TestBed.createComponent(ImageCardComponent );
component = fixture.componentInstance;
component.imageUrl= imageUrl;
);
it('should have image url info', () =>
expect(component.imageUrl).toBeTruthy(); // whatever you have to check for..
);
imageUrl
is not initialized with a value that means it will be undefined if you try to get a value from it before it has a value.
You should be mocking the objects needed in your tests and then write your test cases.
const imageUrl = image: 'testImage.png', url:'image_url';
Now After TestBed set up, have a beforeEach block to mock the needed inputs:
beforeEach(() =>
const fixture = TestBed.createComponent(ImageCardComponent );
component = fixture.componentInstance;
component.imageUrl= imageUrl;
);
it('should have image url info', () =>
expect(component.imageUrl).toBeTruthy(); // whatever you have to check for..
);
answered Mar 8 at 14:41
nircraftnircraft
1,9881524
1,9881524
Turns out I made a typo in my question. Though I tried the general idea you are getting at to no avail
– Tyler B. Joudrey
Mar 8 at 15:05
Can you share the test code here if possible to see what's going on?
– nircraft
Mar 8 at 15:07
I added the test code, and the mock for an image
– Tyler B. Joudrey
Mar 8 at 15:10
Once you docomponent.image = getImageGetResponseMock().image;
you should runfixture.detectChanges();
to make sure your changes are available to be used
– nircraft
Mar 8 at 15:23
1
Here are few good reads: medium.com/frontend-fun/… scotch.io/tutorials/…
– nircraft
Mar 8 at 15:53
|
show 1 more comment
Turns out I made a typo in my question. Though I tried the general idea you are getting at to no avail
– Tyler B. Joudrey
Mar 8 at 15:05
Can you share the test code here if possible to see what's going on?
– nircraft
Mar 8 at 15:07
I added the test code, and the mock for an image
– Tyler B. Joudrey
Mar 8 at 15:10
Once you docomponent.image = getImageGetResponseMock().image;
you should runfixture.detectChanges();
to make sure your changes are available to be used
– nircraft
Mar 8 at 15:23
1
Here are few good reads: medium.com/frontend-fun/… scotch.io/tutorials/…
– nircraft
Mar 8 at 15:53
Turns out I made a typo in my question. Though I tried the general idea you are getting at to no avail
– Tyler B. Joudrey
Mar 8 at 15:05
Turns out I made a typo in my question. Though I tried the general idea you are getting at to no avail
– Tyler B. Joudrey
Mar 8 at 15:05
Can you share the test code here if possible to see what's going on?
– nircraft
Mar 8 at 15:07
Can you share the test code here if possible to see what's going on?
– nircraft
Mar 8 at 15:07
I added the test code, and the mock for an image
– Tyler B. Joudrey
Mar 8 at 15:10
I added the test code, and the mock for an image
– Tyler B. Joudrey
Mar 8 at 15:10
Once you do
component.image = getImageGetResponseMock().image;
you should run fixture.detectChanges();
to make sure your changes are available to be used– nircraft
Mar 8 at 15:23
Once you do
component.image = getImageGetResponseMock().image;
you should run fixture.detectChanges();
to make sure your changes are available to be used– nircraft
Mar 8 at 15:23
1
1
Here are few good reads: medium.com/frontend-fun/… scotch.io/tutorials/…
– nircraft
Mar 8 at 15:53
Here are few good reads: medium.com/frontend-fun/… scotch.io/tutorials/…
– nircraft
Mar 8 at 15:53
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55065265%2fkarma-typeerror-caused-by-a-typeless-angular-component-input%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown