You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: en/quality-code/testing.wiki
+20-22
Original file line number
Diff line number
Diff line change
@@ -18,17 +18,17 @@ Let's start out by creating a simple test case as a working example. Our first w
18
18
19
19
{{{
20
20
$ cd /path/to/lithium/app
21
-
$ li3 create model Post
21
+
$ li3 create model Posts
22
22
23
-
Post created in app\models.
23
+
Posts created in app\models.
24
24
}}}
25
25
26
26
We can also use the `li3 create` command to create our test case class.
27
27
28
28
{{{
29
-
$ li3 create test model Post
29
+
$ li3 create test model Posts
30
30
31
-
PostTest created for Post in app\tests\cases\models.
31
+
PostsTest created for Posts in app\tests\cases\models.
32
32
}}}
33
33
34
34
Doing so creates a test file template class that extends `lithium\test\Unit` and looks like the following:
@@ -38,9 +38,9 @@ Doing so creates a test file template class that extends `lithium\test\Unit` and
38
38
39
39
namespace app\tests\cases\models;
40
40
41
-
use app\models\Post;
41
+
use app\models\Posts;
42
42
43
-
class PostTest extends \lithium\test\Unit {
43
+
class PostsTest extends \lithium\test\Unit {
44
44
45
45
public function setUp() {}
46
46
@@ -53,7 +53,7 @@ class PostTest extends \lithium\test\Unit {
53
53
54
54
The two initial methods supplied act as they're named. The `setUp()` method is used to perform any preparation work you'll need to perform your unit testing logic. This might be anything from setting up database connections to initializing mock data. Similarly, `tearDown()` is used to clean up anything that might be left over once a unit test has been completed. These methods are called before and after each method in your unit test case.
55
55
56
-
The meat of the unit test, however, will be housed inside of methods you create. Each piece of your unit testing logic should be placed inside of a method whose name starts with 'test'. Before we make any adjustments to the `Post` model, let's exercise a bit of TDD and write an example test method first.
56
+
The meat of the unit test, however, will be housed inside of methods you create. Each piece of your unit testing logic should be placed inside of a method whose name starts with 'test'. Before we make any adjustments to the `Posts` model, let's exercise a bit of TDD and write an example test method first.
57
57
58
58
Since our test case is a subclass of `lithium\test\Unit`, we have easy access to a number of methods that help us validate test assertions. Since they're plainly named, I'll list some here. For for information, please refer to the API documentation for `lithium\test\Unit`.
59
59
@@ -69,31 +69,31 @@ Since our test case is a subclass of `lithium\test\Unit`, we have easy access to
69
69
- `assertCookie()`
70
70
- `expectException()`
71
71
72
-
Every post should have a great title, and any editor knows that post titles containing the phrase "top ten" are pure rubbish. We'll eventually need a method in our Post model that searches for this phrase and warns us. Before writing that method, let's establish a test case to cover it. We'll call it `testIsGoodTitle()`. See an example implementation below:
72
+
Every post should have a great title, and any editor knows that post titles containing the phrase "top ten" are pure rubbish. We'll eventually need a method in our Posts model that searches for this phrase and warns us. Before writing that method, let's establish a test case to cover it. We'll call it `testIsGoodTitle()`. See an example implementation below:
73
73
74
74
{{{
75
75
<?php
76
76
77
77
namespace app\tests\cases\models;
78
78
79
-
use app\models\Post;
79
+
use app\models\Posts;
80
80
81
-
class PostTest extends \lithium\test\Unit {
81
+
class PostsTest extends \lithium\test\Unit {
82
82
83
83
public function setUp() {}
84
84
85
85
public function tearDown() {}
86
86
87
87
public function testIsGoodTitle() {
88
-
$this->assertTrue(Post::isGoodTitle("How to Win Friends and Influence People"));
89
-
$this->assertFalse(Post::isGoodTitle("The Top 10 Best Top Ten Lists"));
88
+
$this->assertTrue(Posts::isGoodTitle("How to Win Friends and Influence People"));
89
+
$this->assertFalse(Posts::isGoodTitle("The Top 10 Best Top Ten Lists"));
90
90
}
91
91
}
92
92
93
93
?>
94
94
}}}
95
95
96
-
Turn back to your browser showing the Unit Test Dashboard, and refresh it. You should see a new entry at the top of the list on the left hand side that shows our `PostTest` unit test case. Clicking on the `PostTest` test case should show you the test results. At this point you won't get far—the model will likely complain about a missing connection or function: as it should!
96
+
Turn back to your browser showing the Unit Test Dashboard, and refresh it. You should see a new entry at the top of the list on the left hand side that shows our `PostsTest` unit test case. Clicking on the `PostsTest` test case should show you the test results. At this point you won't get far—the model will likely complain about a missing connection or function: as it should!
97
97
98
98
Let's start working on the model so we can get that test to pass. First, let's specify our model as not having any connection. We'll adjust this later, but let's do this now for simplicity's sake.
99
99
@@ -102,7 +102,7 @@ Let's start working on the model so we can get that test to pass. First, let's s
102
102
103
103
namespace app\models;
104
104
105
-
class Post extends \lithium\data\Model {
105
+
class Posts extends \lithium\data\Model {
106
106
protected $_meta = array('connection' => false);
107
107
public $validates = array();
108
108
}
@@ -117,7 +117,7 @@ Once that's in place, running the test again should have it barking about how `i
117
117
118
118
namespace app\models;
119
119
120
-
class Post extends \lithium\data\Model {
120
+
class Posts extends \lithium\data\Model {
121
121
protected $_meta = array('connection' => false);
122
122
public $validates = array();
123
123
@@ -135,9 +135,9 @@ At this point, your test cases should run successfully in the Unit Test Dashboar
135
135
136
136
Mocks are used in place of actual sources of information. You can create a mock for just about anything: a data source, model data, a console command response... anything. Since we're dealing primarily with the model in this example, let's continue that train of thought, and use some mocks to help us test our new model functionality.
137
137
138
-
Let's create a MockPost that returns test data we can use to run through our `isGoodTitle()` method. One easy way to do that is to create a new class that just returns a RecordSet (in the case of an SQL database) or a Document (in the case of a document database) collection.
138
+
Let's create a MockPosts that returns test data we can use to run through our `isGoodTitle()` method. One easy way to do that is to create a new class that just returns a RecordSet (in the case of an SQL database) or a Document (in the case of a document database) collection.
139
139
140
-
Start by creating a new file in `app/tests/mocks/data/MockPost.php`:
140
+
Start by creating a new file in `app/tests/mocks/data/MockPosts.php`:
0 commit comments