How to use goweb_test_helpers?

17 views
Skip to first unread message

Kevin Fourie

unread,
Oct 23, 2012, 1:56:43 PM10/23/12
to golang...@googlegroups.com
Hi,

New to golang and goweb but seriously loving both so far.

I am trying to test a little goweb API using the goweb_test_helpers but am just not getting anywhere. The API works fine with curl...


HTTP/1.1 200 OK
Content-Type: application/json
Date: Tue, 23 Oct 2012 17:31:56 GMT
Transfer-Encoding: chunked

{"C":"","S":200,"D":[{"firstName":"Kevin","middleName":"G","lastName":"Fourie"}],"E":null}

My test func is as follows...

func TestUserById(t *testing.T) {
log.SetFlags(0)
log.Println("TEST UserById")

MakeTestContext("GET", testDomain+"/v1/users/508181cc2341ab4f5d3e704a")
assertResponse(t, "something")
assertResponseStatus(t, 200)

}

The output of 'go test' is...

$ go test

TEST UserById
currentRequest: &{GET http://localhost:8080/v1/users/508181cc2341ab4f5d3e704a HTTP/1.1 1 1 map[] <nil> 0 [] false localhost:8080 map[] <nil> map[]   <nil>}
currentResponse: &{0  map[]}
--- FAIL: TestUserById (0.00 seconds)
test_helpers.go:82: Response incorrect.
Expected: "something"
Actual:   ""
test_helpers.go:53: HTTP status code expected to be 200, not 0
FAIL

What am I doing wrong?

Thanks
Kevin

matryer

unread,
Oct 25, 2012, 3:15:09 PM10/25/12
to golang...@googlegroups.com
After making your Context, you need to call the func you are testing.  Either use the return of MakeTestContext or use the currentContext variable directly.

E.g.

func MyFunc(c *goweb.Context) {
  // do something
}

// then in your test code
c := MakeTestContext(...)
MyFunc(c)

assert.Contains(t, currentResponse.Output, "[something to check for]")

NOTE: The 'assert' functionality comes from our https://github.com/stretchrcom/testify package.

Hope that helps

Mat

Kevin Fourie

unread,
Oct 26, 2012, 8:59:29 AM10/26/12
to golang...@googlegroups.com
Thanks Mat! That was just the push my tired brain needed. It's working perfectly now.

Kevin
Reply all
Reply to author
Forward
0 new messages