1Test::Mojo(3) User Contributed Perl Documentation Test::Mojo(3)
2
3
4
6 Test::Mojo - Testing Mojo!
7
9 use Test::More tests => 10;
10 use Test::Mojo;
11
12 my $t = Test::Mojo->new(app => 'MyApp');
13
14 $t->get_ok('/welcome')
15 ->status_is(200)
16 ->content_like(qr/Hello!/, 'welcome message!');
17
18 $t->post_form_ok('/search', {title => 'Perl', author => 'taro'})
19 ->status_is(200)
20 ->content_like(qr/Perl.+taro/);
21
22 $t->delete_ok('/something')
23 ->status_is(200)
24 ->header_is('X-Powered-By' => 'Mojolicious (Perl)')
25 ->content_is('Hello world!');
26
28 Test::Mojo is a collection of testing helpers for everyone developing
29 Mojo and Mojolicious applications.
30
32 Test::Mojo implements the following attributes.
33
34 "app"
35 my $app = $t->app;
36 $t = $t->app(MyApp->new);
37
38 Application to be tested.
39
40 "client"
41 my $client = $t->client;
42 $t = $t->client(Mojo::Client->new);
43
44 Client used for testing.
45
46 "tx"
47 my $tx = $t->tx;
48 $t = $t->tx(Mojo::Transaction::Simple->new);
49
50 Current transaction.
51
52 "max_redirects"
53 my $max_redirects = $t->max_redirects;
54 $t = $t->max_redirects(3);
55
56 Maximum number of redirects, defaults to 0.
57
59 Test::Mojo inherits all methods from Mojo::Base and implements the
60 following new ones.
61
62 "content_is"
63 $t = $t->content_is('working!');
64 $t = $t->content_is('working!', 'right content!');
65
66 Check response content for exact match.
67
68 "content_like"
69 $t = $t->content_like(qr/working!/);
70 $t = $t->content_like(qr/working!/, 'right content!');
71
72 Check response content for similar match.
73
74 "content_type_is"
75 $t = $t->content_type_is('text/html');
76
77 Check response content type for exact match.
78
79 "content_type_like"
80 $t = $t->content_type_like(qr/text/);
81 $t = $t->content_type_like(qr/text/, 'right content type!');
82
83 Check response content type for similar match.
84
85 "delete_ok"
86 $t = $t->delete_ok('/foo');
87 $t = $t->delete_ok('/foo', {Expect => 'fun'});
88 $t = $t->delete_ok('/foo', 'Hi!');
89 $t = $t->delete_ok('/foo', {Expect => 'fun'}, 'Hi!');
90
91 Perform a "DELETE" request.
92
93 "element_exists"
94 $t = $t->element_exists('div.foo[x=y]');
95 $t = $t->element_exists('html head title', 'has a title');
96
97 Checks for existence of the CSS3 selectors XML/HTML element. Note that
98 this method is EXPERIMENTAL and might change without warning!
99
100 "get_ok"
101 $t = $t->get_ok('/foo');
102 $t = $t->get_ok('/foo', {Expect => 'fun'});
103 $t = $t->get_ok('/foo', 'Hi!');
104 $t = $t->get_ok('/foo', {Expect => 'fun'}, 'Hi!');
105
106 Perform a "GET" request.
107
108 "head_ok"
109 $t = $t->head_ok('/foo');
110 $t = $t->head_ok('/foo', {Expect => 'fun'});
111 $t = $t->head_ok('/foo', 'Hi!');
112 $t = $t->head_ok('/foo', {Expect => 'fun'}, 'Hi!');
113
114 Perform a "HEAD" request.
115
116 "header_is"
117 $t = $t->header_is(Expect => 'fun');
118
119 Check response header for exact match.
120
121 "header_like"
122 $t = $t->header_like(Expect => qr/fun/);
123 $t = $t->header_like(Expect => qr/fun/, 'right header!');
124
125 Check response header for similar match.
126
127 "json_content_is"
128 $t = $t->json_content_is([1, 2, 3]);
129 $t = $t->json_content_is([1, 2, 3], 'right content!');
130
131 Check response content for JSON data.
132
133 "post_ok"
134 $t = $t->post_ok('/foo');
135 $t = $t->post_ok('/foo', {Expect => 'fun'});
136 $t = $t->post_ok('/foo', 'Hi!');
137 $t = $t->post_ok('/foo', {Expect => 'fun'}, 'Hi!');
138 $t = $t->post_ok('/foo', 'Hi!', 'request worked!');
139
140 Perform a "POST" request.
141
142 "post_form_ok"
143 $t = $t->post_form_ok('/foo' => {test => 123});
144 $t = $t->post_form_ok('/foo' => 'UTF-8' => {test => 123});
145 $t = $t->post_form_ok('/foo', {test => 123}, {Expect => 'fun'});
146 $t = $t->post_form_ok('/foo', 'UTF-8', {test => 123}, {Expect => 'fun'});
147 $t = $t->post_form_ok('/foo', {test => 123}, 'Hi!');
148 $t = $t->post_form_ok('/foo', 'UTF-8', {test => 123}, 'Hi!');
149 $t = $t->post_form_ok('/foo', {test => 123}, {Expect => 'fun'}, 'Hi!');
150 $t = $t->post_form_ok(
151 '/foo',
152 'UTF-8',
153 {test => 123},
154 {Expect => 'fun'},
155 'Hi!'
156 );
157
158 Submit a "POST" form.
159
160 "put_ok"
161 $t = $t->put_ok('/foo');
162 $t = $t->put_ok('/foo', {Expect => 'fun'});
163 $t = $t->put_ok('/foo', 'Hi!');
164 $t = $t->put_ok('/foo', {Expect => 'fun'}, 'Hi!');
165
166 Perform a "PUT" request.
167
168 "reset_session"
169 $t = $t->reset_session;
170
171 Reset user agent session.
172
173 "status_is"
174 $t = $t->status_is(200);
175
176 Check response status for exact match.
177
178 "text_is"
179 $t = $t->text_is('div.foo[x=y]' => 'Hello!');
180 $t = $t->text_is('html head title' => 'Hello!', 'right title');
181
182 Checks text content of the CSS3 selectors XML/HTML element for exact
183 match. Note that this method is EXPERIMENTAL and might change without
184 warning!
185
186 "text_like"
187 $t = $t->text_like('div.foo[x=y]' => qr/Hello/);
188 $t = $t->text_like('html head title' => qr/Hello/, 'right title');
189
190 Checks text content of the CSS3 selectors XML/HTML element for similar
191 match. Note that this method is EXPERIMENTAL and might change without
192 warning!
193
195 Mojolicious, Mojolicious::Guides, <http://mojolicious.org>.
196
197
198
199perl v5.12.3 2010-08-12 Test::Mojo(3)