Mocking object instanciated inside a method

102 views
Skip to first unread message

Hadrien TULIPE

unread,
Jun 20, 2013, 1:02:23 PM6/20/13
to mock...@googlegroups.com
Hi all,

I am having a bit of a struggle here, maybe someone can help me. I wan to test a function in a class:

public function load():void {
if(this._req || !this._onlineResource)
return;
this._req = new DataRequest(this._onlineResource,onSuccess, onFailure);
this._req.proxy = this._proxy;
this._req.send();
}

I want to test that request is being sent and that it contains the proxy. For that, I want to make a mock of DataRequest an use spies. The only problem is that the "_req" object is instantiated inside the "load" method, henc i can't use a mock. I think this is a common issue in mocking but I can't see any solution.

Any ideas?

Thanks

Hob Spillane

unread,
Jun 20, 2013, 3:22:20 PM6/20/13
to mock...@googlegroups.com
Hi Hadrien,

This is a pretty common problem and there are a few ways to deal with it.  For testing purposes, the simplest approach is probably to encapsulate the creation of that object in a protected method and then create a sub-class of your test class for testing which overrides the method that creates the object and returns a mock:

-------------------------
this._req = this.createRequest(this._onlineResources, onSuccess, onFailure);
...

protected function createRequest(...):DataRequest {
   return new DataRequest(...);
}
-------------------------

Then, you can create an inner class in your test case which overrides that behavior:

-------------------------
package {
    public class SomeTest {
        @Mock
        public var dataRequset:DataRequest;

        private var subject:YourTestSubject

        @Before
        public function setup() {
            this.subject = new Testable(dataRequest);
        }
    }
}
class Testable extends YourTestSubject {
    private request:DataRequest;
    public Testable(request:DataRequest) {
        super();
        this.request = request;
    }
    override protected function createRequest() {
        return this.request;
    }
}
-------------------------

Like I said, this is only one way to accomplish this.  There's the whole injection approach, but that involves integrating an injection framework like Parsley or Robotlegs.  You could also create a Provider class who's job is simply to create your request instances for you, and then swap ou providers at test time.



--
You received this message because you are subscribed to the Google Groups "Mockolate" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockolate+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Hadrien TULIPE

unread,
Jun 21, 2013, 4:58:59 AM6/21/13
to mock...@googlegroups.com
Thanks Hob,

This is more or less what I had in mind. I was a bit reluctant to do so because it means modifying the code to make it fit the tests which seems to me not be the right philosophy. Anyhow, this works great so I'll keep that.

Cheers
Reply all
Reply to author
Forward
0 new messages