File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -101,3 +101,31 @@ func TestShouldRespondWithErrorOnFailure(t *testing.T) {
101
101
t .Errorf ("there were unfulfilled expectations: %s" , err )
102
102
}
103
103
}
104
+
105
+ func TestNoPostsReturned (t * testing.T ) {
106
+ mock , err := pgxmock .NewConn ()
107
+ if err != nil {
108
+ t .Fatalf ("an error '%s' was not expected when opening a stub database connection" , err )
109
+ }
110
+ defer mock .Close (context .Background ())
111
+
112
+ // create app with mocked db, request and response to test
113
+ app := & api {mock }
114
+ req , err := http .NewRequest ("GET" , "http://localhost/posts" , nil )
115
+ if err != nil {
116
+ t .Fatalf ("an error '%s' was not expected while creating request" , err )
117
+ }
118
+ w := httptest .NewRecorder ()
119
+
120
+ mock .ExpectQuery ("^SELECT (.+) FROM posts$" ).WillReturnRows (mock .NewRows ([]string {"id" , "title" , "body" }))
121
+ // now we execute our request
122
+ app .posts (w , req )
123
+ if w .Code != 200 {
124
+ t .Fatalf ("expected status code to be 200, but got: %d\n Body: %v" , w .Code , w .Body )
125
+ }
126
+
127
+ // we make sure that all expectations were met
128
+ if err := mock .ExpectationsWereMet (); err != nil {
129
+ t .Errorf ("there were unfulfilled expectations: %s" , err )
130
+ }
131
+ }
You can’t perform that action at this time.
0 commit comments