Running frisby test when promise is fulfilled

Let's assume you have an API with users and categories. Each user must have a category and status.

By now, you should have something like

frisby.create("Get categories")
      .get("http://api/category")
      .expectStatus(200)
      .expectJSON("*",{...})
      .afterJSON(function(categories)
      {   
          frisby.create("Get status list")
                .get("http://api/status")
                .expectStatus(200)
                .expectJSON("*",{...})
                .afterJSON(function(statusList){
                     var luckyCategory = chooseLuckyCategory(categories);
                     frisby.create("Create new user")
                           .post(
                               "http://api/user", 
                               { 
                                 name : "John", 
                                 category : luckyCategory 
                               })
                           .expectStatus(202)
                           .toss();
                 })
                 .toss();

      })
     .toss();

      

It's horrible. If I need a new test that requires getting my categories, I have to repeat almost all of the code above, or include a new test inside them. It won't be easy, or I'll have to repeat myself.

Something like the following would be much better:

var categoriesP = q.defer();
var statusListP = q.defer();
var categories, statusList;

frisby.create("Get categories")
      .get("http://api/category")
      .expectStatus(200)
      .expectJSON("*",{...})
      .afterJSON(function(result)
      {   
          categories = result;
          categoriesP.resolve(result);
      })
     .toss();


frisby.create("Get status list")
    .get("http://api/status")
    .expectStatus(200)
    .expectJSON("*",{...})
    .afterJSON(function(result){
         statusList = result;
         statusListP.resolve(result);
     })
    .toss();

q.all([categoriesP.promise, statusListP.promise])
 .then(function()
  {
     var luckyCategory = chooseLuckyCategory(categories);
     var happyStatus = chooseHappyStatus(status);
     frisby.create("Create new user")
           .post(
               "http://api/user", 
               { 
                 name : "John", 
                 category : luckyCategory 
               })
           .expectStatus(202)
           .toss();
  });

      

The code is less complicated and I have promises that I could reuse. I could even create a nodejs module to hold all the promises for things like category and status, which will be needed later. The main problem is that jasmine kills everything once all failed tests are satisfied or rejected. But this does not give enough time for the promises to execute.

+3


source to share


1 answer


If you really don't need to test in javascript, you can try using Airborne.rb . It's like Frisby with no async issues.



+2


source







All Articles