When are static properties initialised in testing in Swift? - swift

I have a class with an optional type property (static). I set this when I start to use the class. Obviously, when the program first starts, the property has no value.
My question is about testing. I expected the class to be initialised at the start of every test (i.e. the type property set back to having no value). However, it seems the class is initiated once at the beginning of all my tests and therefore the type property has the same value in all tests, which is not what I want.
What are the rules about class initialisation in testing? Is there any way I can force my class to "reset"?
Thanks,
Julian

As you've noticed, static properties on your test case are persistent throughout all your tests. However, XCTestCase has two sets of methods that you can override to customize this, called setUp() and tearDown(). It's somewhat confusing, since there are identically named class and instance methods for each of these; override the class methods to have something happen only once, and override the instance methods to make something happen before or after each individual test. In your case, it would probably make the most sense to override the instance method version of setUp() and have it reset your properties.

Related

Swift. Let to override but not to call the method

Is there any way to let the method of the superclass be overrided, but not called directly?
For example: A inherited from B. There is two methods. One is final and must be called, second is overridable but shouldn't be called, only override.
I tried #available and private but that don't fit. I think that it can be reached by delegate, but maybe there is another way?
For example, you can throw an error in your method that will say that this method shouldn't be called and child class should override it. But, of course, it is no compile time restriction, only runtime.
Also it has sense for you to read discussion here: Abstract functions in Swift Language

Swift - what should the default values of properties be in the parent class?

Not sure if I worded this question correctly, but here's my issue: I have a base class and a subclass, and my base class should never be instantiated on its own (in other languages it would be abstract). I know abstract classes aren't a thing in Swift. I have some computed read-only properties that change what they return in each subclass; they are more or less customized constants. Firstly, are overridden computed properties the best way to handle this kind of thing? Secondly, if these variables need to get initialized, i.e. can't be nil, what should they be initialized to in the parent class? Is there a way to otherwise indicate that the parent class shouldn't be initialized on its own?
You probably should use protocol instead of base class in your case. All common implementation can be done in protocol extensions and you won't need to provide default values for constants - just specify required get methods in the protocol.

Different field instances in class and parent/Call super constructor with method

I am trying to call the super constructor from a class using a method. The whole setup looks like this:
class Straight(hand: Hand) extends Combination(Straight.makeHandAceLowIfNeeded(hand), 5)
object Straight {
private def makeHandAceLowIfNeeded(hand: Hand): Hand = {
...
}
}
While this does compile, it has some rather odd runtime behaviour. While debugging, I noticed that the Straight instances have the "hand" property defined twice. Can somebody tell me what is going on, and what the proper way is to call the super constructor with different arguments?
In my use case, I want to call the super constructor with a modified hand in which I replaced a card compared to the original constructor argument.
Debugger screenshot with duplicate field:
.
It's a perfectly fine way to call the superclass constructor. These are two private fields and they don't conflict, though you can rename one of them to avoid confusion during debugging (or if you want to access the superclass' value from the subclass). However, the field should only be generated for a class parameter if it's used outside a constructor, and in your case it doesn't appear to be. Did you simplify the definition of Straight?

Swift: class func .... why use this instead of func when creating a method inside a class?

I'm new to coding, apologies for dumb question.
Am following a tutorial to build a note taking app using Swift in Xcode.
Within a class definition I have been defining methods using the keyword func myMethod etc. At one point the instructor decides to define a Class method (within the existing class) using class func myMethod.
Why would you do this?
Thanks in advance for any feedback.
By defining a class method it means that you don't need an instance of that class to use the method. So instead of:
var myInstance: MyClass = MyClass()
myInstance.myMethod()
You can simply use:
MyClass.myMethod()
The static (class) function is callable without needing an instance of the class available; it may be called without having to instantiate an object.
This can be useful for encapsulation (avoiding placing the function in the global namespace), or for operations that apply to all objects of a given class, such as tracking the total number of objects currently instantiated.
Static functions can be used to define a namespaces collection of related utility functions:
aDate = Utils.getDate()
aTime = Utils.getTime()
Another common use is for the singleton pattern, where a static function is used to provide access to an object that is limited to being instantiate only once:
obj = MySingleton.getInstance()
obj.whatever()
One answer is namespacing. If a function is only relevant to a certain class there is no need to declare the function globally.
This is Swift's take on static methods:
Static methods are meant to be relevant to all the instances of a class (or no instances) rather than to any specific instance.
An example of these are the animation functions in UIView, or the canSendMail function from MFMailComposeViewController.

SenTest, Objective-C, Category Weirdness

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:
Set a var in the testFoo method.
In testFoo, call my mock controller's overridden method.
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?
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.