简体   繁体   中英

Run frisby test when promise is satisfied

Suppose you have an API with users and categories. Each user must have a category and a status.

Currently, you must 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();

That is horrible. If I need a new test that requires retrieving my categories I must repeat almost all the code above or chain the new test inside them. It will not be simple or I will need to repeat myself.

It will be a lot better something like the following:

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 complex and I have promises that I could reuse. I could even create a nodejs module to hold all the promises for stuff like category and status that are will be needed later. The main problem is that jasmine kills all the processes as soon as all the tossed tests are satisfied or rejected. But that doesn't give enough time to the promises to be fulfilled.

Unless you really need to test in javascript, you can try using Airborne.rb . Its similar to Frisby just without the asynchrony issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM