0
votes

I like to perform E2E testing using a real http backend.

As part of my E2E testing I want to create a test user in a beforeEach() function, and I would remove this user in an afterEach() function when I'm done testing.

So I have created a REST endpoint for creating users: /users/create And then I need to post a username and password to that Url.

But it seems it is not possible to inject the $http service in E2E tests for some reason.

So how can I make a POST request to /users/create in an E2E beforeEach() function?

1

1 Answers

1
votes

The e2e testing environment doesn't have access to either jQuery or Angular functions by default.

I would add jQuery to the testing environment. You can do this by following lorcan's answer (not currently the top answer) to this question: How to execute jQuery from Angular e2e test scope?

Once you have jQuery available, you can do a standard ajax request:

beforeEach(function() {
    $.ajax({ url:"/your/function/here", async:false,
        success: function(data) {
            alert(data);
    }});
});

Make sure you set async to be false. You'll probably want to do something different with the success function, like assigning the return data to a variable.