0

I have a SenTest class I am attempting to write which uses a Category to override a method in a controller class. I want my mock controller class to call a method in the SenTest class to determine how to behave for various tests.

For example:

  1. Set a var in the testFoo method.
  2. In testFoo, call my mock controller's overridden method.
  3. Have it call back up to the SenTest singleton to figure out how to behave for testFoo.

Is this possible, or am I an idiot?

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Genericrich
  • 4,611
  • 5
  • 36
  • 55
  • What’s the category for here? To turn the controller into a mock? That is, is the override the bit that’s going up to the test singleton? – Ben Stiglitz Apr 20 '11 at 22:24
  • The category is to override a method in my controller that makes a network connection. I want to avoid this in my unit tests. – Genericrich Apr 21 '11 at 11:17
  • I think I *may* want to use "Associative References" here, which would let me, say, set a dictionary of test parameters on my controller under test, and then retrieve it in the category override method to fetch the proper data to drive a given test. Will try this today. – Genericrich Apr 21 '11 at 11:23
  • Sounds to me like you want to either move the network bit out into a separate object (via the strategy pattern) or subclass the controller for your tests and use plain old ivars. – Ben Stiglitz Apr 21 '11 at 18:18

1 Answers1

0

Turns out the trick to doing this is to to make the associated reference IN the category code, not in the SenTest case.

So for example, declare this in category:

 -(void)fakeMethodToSetTestData:(NSDictionary *)data;

And in that method set the associative reference to store your data "associated" with "self".

Call this in your test case on your object, passing in your data.

Then in your test case, call your:

-(void)realMethodInCategoryWhichIsBeingOverridden;

And in your category implementation of this method, retrieve the data from the associative reference you set in the first method.

This will let you set test data on a Controller so that you can hijack the delegation chain, for example, to prevent the second method from going to the network and returning your test data into your controller's code.

Genericrich
  • 4,611
  • 5
  • 36
  • 55