I want to add dummy objects for testing in project, but I don't want them to be included in my final build. So I have my AppDelegate
class and there in imports:
#ifdef TESTING
#import "DummyBeaconLocationManager.h"
#else
#import "BeaconLocationManager.h"
#endif
And later:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...
#ifdef TESTING
[[DummyBeaconLocationManager sharedInstance] startRanging];
#else
self.beaconLocationManager = [BeaconLocationManager sharedInstance];
[self.beaconLocationManager startRanging];
#endif
return YES;
}
But the problem is that I have to include this in my Target Membership
, not my test target. Is there a way to not include these files in my main target, but only in the Test
target?
Edit: Whats need to do is test my app after launch. I want to test it on simulator but app using beacons. So I created dummy objects that represent beacons and simulate location manager. When the app starts with TESTING option it not start ranging beacon but put a dummy objects as a beacon instead.
A dummy object is the simplest object available: it is a point with orientation, and it can be seen as a reference frame.
Mockito mock method We can use Mockito class mock() method to create a mock object of a given class or interface. This is the simplest way to mock an object. We are using JUnit 5 to write test cases in conjunction with Mockito to mock objects.
Creating mock objects with the Mockito APIclass) extension for JUnit 5 in combination with the @Mock annotation on fields. Using the static mock() method. Using the @Mock annotation.
Once you follow these steps, you will be able to add test classes with test functionality to your build. To run your app using the test functionality, you should select the Testing scheme that is configured below.
To allow conditional imports and functionality to be effective for testing, you will need the following ingredients:
1. Duplicate a Configuration for testing
With the project file selected in the Project Navigator, follow these steps:
Select Duplicate "Debug" Configuration
Rename the new configuration, and drag it to re-order
2. Duplicate a Target for testing
Now that you have a testing configuration, add a testing target. With the project file select in the project navigator, follow these steps:
Right-click or Option-click an existing target, and select Duplicate.
Tap on the new target to rename it, then drag it to reorder your targets.
3. Manage Schemes
Now that you have a testing target and configuration, you are ready to add a scheme that points to the new target and configuration.
Tap on the schemes (next to the stop button), and select Manage Schemes...
In the Schemes manager popup, if you have elected to autocreate schemes, the new scheme will already be listed. Otherwise, you can tap + in the popup to add a scheme. Each scheme can be shared, as shown here:
Tap on the new scheme to rename it, and drag it to reorder:
To edit the new scheme, ensure that it is selected, and tap Edit...
In the edit panel, select the Testing build configuration
Ensure that the scheme points to the Testing Target, by tapping on the Executable drop-down:
Ensure the scheme is pointing to the correct build configuration, for other actions, such as Automated Testing:
4. Configure the build settings for your targets
Now that you have your testing scheme set up to point to the Testing configuration, it will behave exactly like the Debug configuration, until you modify the build settings. Follow these steps, to change build settings for your Testing configuration:
For most build settings, there is an option for each configuration. When choosing which configuration to use in your settings, ensure the main target is selected:
The Preprocessor Macros are under the section titled 'Apple LLVM 7.0 - Preprocessing':
Tap on a row, to select it, tap the enter key to edit and commit your changes, use the arrow keys to move up or down. It is a good practice to define all your preprocessor macros for all configurations, like this:
5. Add a class to your Testing target
Now, the Testing Scheme points to a configuration that behaves differently from your Debug configuration. You should now be able to select the Testing scheme from the schemes dropdown menu, and run your Testing configuration.
You can modify the target membership of a class in one of two ways.
Schemes are usually paired to build configurations. One good practice is to have a scheme/configuration for each audience that needs a different version of your build. Here are some basic audiences that typically need separate configurations:
If you want to modify any functionality in testing mode, you could use a subclass, and only add the subclass to your testing target.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With