简体   繁体   English

当承诺满足时运行frisby测试

[英]Run frisby test when promise is satisfied

Suppose you have an API with users and categories. 假设您有一个包含用户和类别的API。 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. 我甚至可以创建一个nodejs模块来保存稍后将需要的类别和状态等所有的承诺。 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 . 除非你真的需要在javascript中测试,否则你可以尝试使用Airborne.rb Its similar to Frisby just without the asynchrony issues. 它类似于弗里斯比,没有异步问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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