弗里斯比functiontesting标准

我是新来的,我一直在寻找方法(或标准)来编写适当的functiontesting,但我仍然有许多没有答案的问题。 我使用FrisbyJS为我的NodeJS API应用程序和jasmine-node编写functiontesting来运行它们。

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

这是一个场景:

  • 客人可以创build一个User 。 (显然,不允许用户名重复)
  • 创build一个User ,他可以login。 成功login后,他获得一个Access-Token。
  • User可以创build一个Post 。 然后一个Post可以有Comment ,等等…
  • User创build后不能删除。 (不是从我的NodeJS应用程序)

弗里斯比文档说的是,我应该在testing中写一个testing。

例如(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(); 

这是写functiontesting的正确方法吗? 我不这么认为…看起来很脏。

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

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

这是使用NodeJS的产品。 这是我遗憾地决定frisby的一个很大的原因。 这一点,事实上我找不到一个好的方法来加载数据库中的预期结果,以便在testing中使用它们。

从我的理解 – 你基本上想要按顺序执行你的testing用例。 一个接一个地。

但是由于这是JavaScript,frisbytesting用例是asynchronous的。 因此,为了使它们同步,文档build议你嵌套testing用例。 现在对于几个testing用例来说可能是可以的。 但如果有数百个testing用例,嵌套将会变得混乱。

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

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);