0

I need to create a user in kinvey and assert that the user has been added, all within an angular test. I am using the karma test runner. All my of tests are timing out, and none of the code inside the kinvey promise blocks is being executed. How can I go about writing these tests? Test code is below:

describe("Kinvey: Users", function() {

var key,
    secret;

beforeEach(function(){
    key = '...',
    secret = '...'


});


it("should create a user", function(done){


    var App = angular.module('App', ['kinvey']);

    App.run(['$kinvey', function($kinvey) {
    $kinvey.init({
        appKey: key,
        masterSecret: secret
    });

        var promise = $kinvey.DataStore.save('users', {
        username : 'gertrude@test.com'
        });

        promise().then(function(success){
            var query = new $kinvey.Query();
            query.equalTo('username', 'gertrude');
            var queryPromise = $kinvey.DataStore.find('users', query);
            return queryPromise().then(
                function(response){
                    console.log("response");
                    expect(response.username).to.equal("gertrude@test.com");

                    var destroyPromise = $kinvey.DataStore.destroy('users', response.id);

                    return destroyPromise().then(function(success){
                        return done();
                    }, function(error){
                        return done();
                    })
                }, function(error){
                    return null;
                })
        }, function(error){
            return null;
        });

    }]);
}); 
user3080098
  • 355
  • 1
  • 5
  • 13

1 Answers1

0

You are missing a few things:

  • You are missing a call to angular.bootstrap(), so the run block is never executed.
  • The $kinvey.init() method is asynchronous. So, before calling $kinvey.DataStore.save() (or any other $kinvey.* method for that matter), make sure the init method finishes.
  • It seems you are creating users by using $kinvey.DataStore.save() to the users collection. It is highly recommended that you use $kinvey.User.signup() instead.

The best way to go forward is to move things to the before hook:

before(function(done) {
  this.App = angular.module('App', ['kinvey']);
  this.App.run(['$kinvey', function($kinvey) {
    $kinvey.init({
      appKey    : 'App Key',
      appSecret : 'App Secret'
    }).then(function() {
      done();
    }, function(error) {
      done(new Error(error.description));
    });
  }]);
  angular.bootstrap(document, ['kinvey']);
});

Now, in your test (it method), you can obtain a reference to $kinvey by doing:

var $injector = angular.injector(['ng', 'kinvey']);
$injector.invoke(function($kinvey) {
  // Do your tests with $kinvey.
});

Test away! I put a JSFiddle here.

Disclaimer: I am the author of the library.