【问题标题】:Frisby Functional Test standardsFrisby 功能测试标准
【发布时间】:2015-10-27 13:01:38
【问题描述】:

我是新手,我一直在寻找编写适当功能测试的方法(或标准),但我仍然有许多未解决的问题。我正在使用 FrisbyJS 为我的 NodeJS API 应用程序编写功能测试,并使用jasmine-node 来运行它们。

我浏览了 Frisby 的文档,但对我来说没有什么收获。

这是一个场景:

  • 客人可以创建User。 (显然,不允许用户名重复)
  • 创建User后,即可登录。成功登录后,他会获得一个访问令牌。
  • User 可以创建Post。那么Post 可以有Comment,以此类推...
  • User 一经创建便无法删除。 (不是来自我的 NodeJS 应用程序)

Frisby 文档说的是,我应该在测试中编写测试。

例如(full-test.spec.js):

// Create User Test
frisby.create('Create a `User`')
    .post('http://localhost/users', { ... }, {json: true})
    .expectStatus(200)
    .afterJSON(function (json) {

        // User Login Test
        frisby.create('Login `User`')
            .post('http://localhost/users/login', { ... }, {json: true})
            .expectStatus(200)
            .afterJSON(function (json) {

                // Another Test (For example, Create a post, and then comment)

            })
            .toss();

    })
    .toss();

这是编写功能测试的正确方法吗?我不这么认为......它看起来很脏。

我希望我的测试是模块化的。每个测试的单独文件。 如果我为每个测试创建单独的文件,那么在为Create Post 编写测试时,我需要一个User 的访问令牌。

总而言之,问题是:如果事物相互依赖,我应该如何编写测试? Comment 依赖于 PostPost 依赖于 User

【问题讨论】:

    标签: node.js functional-testing frisby.js


    【解决方案1】:

    这是使用 NodeJS 的副产品。这是我后悔决定选择frisby的一个重要原因。事实是我找不到及时从数据库中加载预期结果以在测试中使用它们的好方法。

    【讨论】:

      【解决方案2】:

      据我了解 - 您基本上希望按顺序执行您的测试用例。一个接一个。

      但由于这是 javascript,frisby 测试用例是异步的。因此,为了使它们同步,文档建议您嵌套测试用例。现在对于几个测试用例来说这可能是可以的。但是如果有数百个测试用例,嵌套就会变得混乱。

      因此,我们使用 sequenty - 另一个 nodejs 模块,它使用回调按顺序执行函数(以及包装在这些函数中的测试用例)。在 afterJSON 块中,在所有断言之后,您必须进行回调 - cb() [通过 sequenty 传递给您的函数]。

      https://github.com/AndyShin/sequenty

      var sequenty = require('sequenty');
      
      function f1(cb){
          frisby.create('Create a `User`')
          .post('http://localhost/users', { ... }, {json: true})
          .expectStatus(200)
          .afterJSON(function (json) {
               //Do some assertions
               cb(); //call back at the end to signify you are OK to execute next test case
          })
          .toss();
      }
      
      function f2(cb){
          // User Login Test
          frisby.create('Login `User`')
              .post('http://localhost/users/login', { ... }, {json: true})
              .expectStatus(200)
              .afterJSON(function (json) {
                    // Some assertions
                    cb(); 
               })
           .toss();
      }
      
      sequenty.run(f1,f2);
      

      【讨论】:

      • NodeJs 有它自己的方式,但由于它被广泛使用,我发现大多数问题的解决方案都很容易获得。现在。
      猜你喜欢
      • 2019-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-29
      • 2014-07-30
      相关资源
      最近更新 更多