I am writing an end to end test using Jasmine for AngularJS. I am using Protractor to run the test. I have the following markup
<ul class="phone-thumbs">
<li ng-repeat="img in phone.images">
<img ng-src="{{img}}">
</li>
</ul>
And I want to test that for a particular page instance I have four of these images. Here's what I have so far
describe('Phone detail view', function() {
beforeEach(function() {
browser.get('app/index.html#/phones/nexus-s');
});
it('should display four thumbnails on the nexus-s page', function() {
expect(element(by.css('.phone-thumbs li img')).length()).toBe(4);
});
});
Problem is that I get an error saying
TypeError: Object #<Object> has no method 'length'
Where am I going wrong?
Jasmine is a behavior driven development framework for JavaScript that has become the most popular choice for testing AngularJS applications. Jasmine provides functions to help with structuring your tests and also making assertions.
Testing in AngularJS is achieved by using the karma framework, a framework which has been developed by Google itself. The karma framework is installed using the node package manager. The key modules which are required to be installed for basic testing are karma, karma-chrome-launcher ,karma-jasmine, and karma-cli.
Similar to Karma, it’s also the recommended testing framework within the Angular documentation as it’s setup for you with the Angular CLI. Jasmine is also dependency free and doesn’t require a DOM. As far as features go, I love that Jasmine has almost everything I need for testing built into it.
We can test this by using the Jasmine function expect (), which will verify whether the incoming output is equal, contain, toBe an expected output or not. It is going to help us to assert that the functionality returns the given output. Finally, our spec file will look as follows: To run the spec file we need to write the command ng test.
In AngularJS, we can perform Unit Testing separately for controllers and directives. We can also perform end of end testing of AngularJS, which is testing from a user perspective. Karma is a testing automation tool created by the Angular JS team at Google.
In the beforeEach function for our test suite, we configure a testing module using the TestBed class. This creates a test Angular Module which we can use to instantiate components, perform dependency injection, and so on.The testBed is a mock environment to run Angular 2+ component tests without the browser.
This question is part of the Experiments section in tutorial 8 of the AngularJS tutorial.
Here is how I solved it. I took the different road of counting the images rendered rather than testing the repeater/model directly.
it('should display four thumbnails of the nexus-s', function() {
var imgsCount = element.all(by.css('.phone-thumbs li img')).count();
expect(imgsCount).toBe(4);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With