Accessing/Reflecting/Mocking private type alias

53 views
Skip to first unread message

Donovan

unread,
Sep 7, 2012, 5:07:36 AM9/7/12
to golan...@googlegroups.com
Hi,

I'm writing some unit tests which run code which depends on the Gorilla Web Toolkit. The tested code requires mux.Vars() to contain some values, but I don't want to invoke ServeHTTP() in these particular tests, which is where mux.Vars() is initialised. The nub of the problem lies with the fact that a private type alias exists in the mux package which is used as the type of the key for the DefaultContext:

Type alias:


mux.Vars() setting code


If I promote contextKey to exported as ContextKey I can get the correct behaviour with this code:
var vars = map[string]string{
"doctype": strconv.FormatUint(uint64(doctype), 10),
"docid":   strconv.FormatUint(uint64(docid), 10),
}
context.DefaultContext.Set(req, mux.ContextKey(0), vars)

but obviously this involves hacking a library package. Without getting Rodrigo to alter the mux API, is there another way I can circumvent this testing issue?

Cheers,
Donovan.

Reply all
Reply to author
Forward
0 new messages