Can I get a value from a test and use it in the next test?

30 views
Skip to first unread message

Nacho B

unread,
Mar 25, 2015, 2:30:02 PM3/25/15
to mojol...@googlegroups.com
I am trying to make tests for a CRUD app. So after testing if a record has been created, I want to test how it is displayed and then test if it can be deleted succesfully.

In the create record function I get the id (using PostgreSql's RETURNING) and a 'OK' through JSON.

I can test those values using this test:

$t->post_ok('/users' => form => {alias => 'TESTING USER ALIAS'})
   
->status_is(200)
   
->header_is('content-type' => 'application/json')
   
->json_has( 'result' => 'OK' )
   
->json_like( 'last_id' => qr/\d/ )
   
;

    
Now I want to make the tests for the display and the deletion, something like:

$t->get_ok('/users/15')
   
->status_is(200)
    ->content_like(qr /TESTING USER ALIAS/)
   
;


But I need the last_id value that returns (15, in my example), a variable that I can somehow chain from a test to the next.


Any suggestions?

Regards:
Nacho B

Alexey Stavrov

unread,
Mar 25, 2015, 3:02:15 PM3/25/15
to mojol...@googlegroups.com
$t->get_ok($your_url)->status_is(...);
my $id = $t->tx->res->json->{your_key};
$t
->get_ok("/users/$id")->status_is(...);



среда, 25 марта 2015 г., 23:30:02 UTC+5 пользователь Nacho B написал:

Nacho B

unread,
Mar 26, 2015, 5:27:02 AM3/26/15
to mojol...@googlegroups.com
Thank you!, Alexey, this is what I need.

Now I have located in the docs (and I have understood) the "Current transaction" attribute. Here is the link, just in caso someone else needs it in the future: http://mojolicio.us/perldoc/Test/Mojo#tx


Regards:
Nacho B.
Reply all
Reply to author
Forward
0 new messages