user2871401
user2871401

Reputation: 1877

End-to-End testing with Jasmine

I am trying to perform some end-to-end tests of an application written with AngularJS. Currently, I have the following tests setup in e2e.tests.js:

describe('MyApp', function() {
    beforeEach(function() {
        browser().navigateTo('../index.html');
    });

    it('should be true', function() {
        expect(true).toBe(true);
    });
});

Oddly, this basic test fails. The test itself runs. However the results are:

203ms   browser navigate to '../index.html'
5ms expect undefined toBe true
           http://localhost:81/tests/e2e.tests.js:10:9
           expected true but was undefined

Considering "true" is hard-coded, I would expect it to pass this test. I have no idea how true can be undefined. What am I missing?

Upvotes: 4

Views: 3763

Answers (2)

Dheeraj Moudgil
Dheeraj Moudgil

Reputation: 51

I too have faced the similar problem and here's what I found. You must be using ng-scenario as your framework with jasmine in config file. The fact is that expect function in ng-scenario doesn't take any var value or Boolean value. It only takes functions like

expect(browser().location().path()).toContain('some/string')

or some other ng-scenario function like

expect(element('some element').html()).toContain('some String');

Any variable value or Boolean in expect function is undefined.

If you want to use Boolean(true/false) or you want your test to be passed then you have to remove 'ng-scenario' from your framework section of config file. Try It with only jasmine!

Upvotes: 0

SleepyMurph
SleepyMurph

Reputation: 2079

The thing about Angular's E2E test framework is that looks like Jasmine, but it is not Jasmine. The E2E test code is actually all asynchronous, but it is written in a clever way so that the calls look normal. Most of the calls create asynchronous tasks and Future objects that are tested later. The Future object is kind of like a promise, but it's a little different. It has a value property that it sets when it's ready, and then it calls a done function to move to the next step. In E2E tests, the expect function takes Future objects, not values. You're seeing undefined because expect is testing against future.value, which in this case is true.value, which is undefined.

Try using one of the available selectors that return futures and then test the result. Something like this:

expect(element("html").text()).toMatch("Our App");

The Future objects are not well documented, but you should be able to create a Future manually like this:

var trueFuture = angular.scenario.Future(
    "a true value",            // name that is displayed in the results
    function(callback) {       // "behavior" function
        callback(null, true);  // supposed to call callback with (error, result)
    });
expect(trueFuture).toEqual(true);

If you look in the ng-scenario source code, you can see the place where the matcher tests future.value in the angular.scenario.matcher function.

Upvotes: 8

Related Questions