Pham's approach to defining these functions as vars and redefining them at
test time?
On Wednesday, August 22, 2012 6:04:15 AM UTC-7, John Beisley wrote:
You could always do something like: http://play.golang.org/p/y3RqWYVtpt
It solves the problem without a global, but does mean splitting your
function into a public function (that simply
wraps the call with normal implementation), and a private function
that uses the implementation it is given.
metaprogramming
some
we'll
But
of
but
this
--You could always do something like: http://play.golang.org/p/y3RqWYVtpt
It solves the problem without a global, but does mean splitting your
function into a public function (that simply
wraps the call with normal implementation), and a private function
that uses the implementation it is given.
On 21 August 2012 17:39, <rock...@google.com <javascript:>> wrote:
But then anyone in the world using your package could re-define
MyFunction
to be whatever they want. This could be a major problem for
encapsulation
and for the integrity of your package. Alternatively, you could have
MyFunction delegate to myFunction and make myFunction a var. Then you can
still use your scheme for testing, and still prevent the rest of the world
from changing your MyFunction. But personally, I never liked redefining
functions and then switching them back because what happens if your test
harness is multi-threaded?
intoBut then anyone in the world using your package could re-define
MyFunction
to be whatever they want. This could be a major problem for
encapsulation
and for the integrity of your package. Alternatively, you could have
MyFunction delegate to myFunction and make myFunction a var. Then you can
still use your scheme for testing, and still prevent the rest of the world
from changing your MyFunction. But personally, I never liked redefining
functions and then switching them back because what happens if your test
harness is multi-threaded?
On Thursday, July 5, 2012 12:41:16 PM UTC-7, Trung Pham wrote:
I figured out how to do what I want in Go. Basically, turn everything
I figured out how to do what I want in Go. Basically, turn everything
a var, and redefine it in my unit test, then restore it afterward.
In myapp.go file
package myapp
var MyFunction = func () int{
//do something very complicated.
}
instead of
func MyFunction() int{
//do something very complicated
}
And in my unit test
import "myapp"
func TestSomething(t testing.T){
oldMyFunction = myapp.MyFunction
myapp.MyFunction = func() int{
//simply return 1;
}
//call something that call myapp.MyFunction
//restore myapp.MyFunction after my test is done
myapp.MyFunction = oldMyFunction
}
It's ugly but it serves my purpose. However, I can do this on packages
that I wrote because other packages do not define their function as
variables.
On Saturday, June 23, 2012 7:32:52 PM UTC-7, sdeg...@8thlight.comwrote:
In myapp.go file
package myapp
var MyFunction = func () int{
//do something very complicated.
}
instead of
func MyFunction() int{
//do something very complicated
}
And in my unit test
import "myapp"
func TestSomething(t testing.T){
oldMyFunction = myapp.MyFunction
myapp.MyFunction = func() int{
//simply return 1;
}
//call something that call myapp.MyFunction
//restore myapp.MyFunction after my test is done
myapp.MyFunction = oldMyFunction
}
It's ugly but it serves my purpose. However, I can do this on packages
that I wrote because other packages do not define their function as
variables.
On Saturday, June 23, 2012 7:32:52 PM UTC-7, sdeg...@8thlight.comwrote:
No, it's not possible because OCMock makes use of ObjC's
abilities to create methods on an object at run-time. There may be
other technique to simplify the redundancy in implementation which
keep seeing, and the community seems to be working on this over time.
for now the solution is to write your own mocks and make careful use
interfaces. This is the only downside to having no meta-programming,
it's totally worth it, because in every other situation, magic sucks.
-Steven
-Steven
On Saturday, June 23, 2012 7:32:37 PM UTC-5, Trung Pham wrote:
Mocking seems to be pretty easy in Objective-C(also compiled) using
Mocking seems to be pretty easy in Objective-C(also compiled) using
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.