首页 文章

Frisby功能测试标准

提问于
浏览
2

我'm new to this and I have been searching for ways (or standards) to write proper functional tests but I still I have many unanswered questions. I'm使用FrisbyJS为我的NodeJS API应用程序编写功能测试,并使用 jasmine-node 来运行它们 .

我已经阅读了弗里斯比的文档,但这对我来说并不富有成效 .

这是一个场景:

  • 来宾可以创建 User . (显然不允许用户名重复)

  • 创建 User 后,他可以登录 . 成功登录后,他获得了一个Access-Token .

  • A User 可以创建 Post . 那么 Post 可以有 Comment ,依此类推......

  • 创建后无法删除 User . (不是来自我的NodeJS应用程序)

弗里斯比文档说的是,我应该在测试中编写一个测试 .

例如(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 的Access-Token .

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

2 回答

  • 0

    这是使用NodeJS的副产品 . 这是我后悔决定弗里斯比的一个重要原因 . 那个和事实我找不到一个很好的方法来及时地从数据库中加载预期的结果,以便在测试中使用它们 .

  • 0

    据我所知 - 您基本上希望按顺序执行测试用例 . 一个接一个地 .

    但由于这是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);
    

相关问题