Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding dummy objects to project

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.

like image 444
Jakub Avatar asked Sep 29 '15 19:09

Jakub


People also ask

What is dummy object?

A dummy object is the simplest object available: it is a point with orientation, and it can be seen as a reference frame.

How do you mock an object?

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.

How do you mock an object in JUnit?

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.


1 Answers

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.

Xcode Project Configuration (using Xcode 7.0.1)

To allow conditional imports and functionality to be effective for testing, you will need the following ingredients:

  1. Testing Configuration
  2. Testing Target
  3. Testing Scheme

Short Explanation of Schemes, Targets and Build Configurations

  • Schemes point to targets and configurations.
  • Targets can be configured with different build settings.
  • Configurations can be added, to branch out target build settings.

Here are the steps:

1. Duplicate a Configuration for testing

With the project file selected in the Project Navigator, follow these steps:

  1. Select the project target
  2. Select Info
  3. Select + to add a configuration

Duplicate a project configuration

  1. Select Duplicate "Debug" Configuration Duplicate "Debug" Configuration

  2. Rename the new configuration, and drag it to re-order Rename project configuration and drag 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:

  1. Right-click or Option-click an existing target, and select Duplicate. Duplicate an existing target

  2. Tap on the new target to rename it, then drag it to reorder your targets. Rename and drag new target

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.

  1. Tap on the schemes (next to the stop button), and select Manage Schemes... Select Manage Schemes...

  2. 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: Configure new scheme

  3. Tap on the new scheme to rename it, and drag it to reorder: Rename and reorder schemes

  4. To edit the new scheme, ensure that it is selected, and tap Edit... Edit scheme

  5. In the edit panel, select the Testing build configuration Select Testing build configuration

  6. Ensure that the scheme points to the Testing Target, by tapping on the Executable drop-down: Point to Testing Target for Run action

  7. Ensure the scheme is pointing to the correct build configuration, for other actions, such as Automated Testing: Point to Testing Target for Test action

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:

  1. 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: Select main target

  2. The Preprocessor Macros are under the section titled 'Apple LLVM 7.0 - Preprocessing': Preprocessor Macros

  3. 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: Define preprocessor macros

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.

  1. When you create a new file, the third panel where you can choose the location, has options at the bottom, for each target:

Choose target membership

  1. When you select a file in your Project Navigator, the File Inspector has a Target Membership panel where you can make modifications: Modify target membership

Schemes

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:

  • Developer > DEBUG
  • Developer > TESTING
  • Internal Testing > DEVELOPMENT
  • Beta Testers / Production > APP STORE

Subclassing

If you want to modify any functionality in testing mode, you could use a subclass, and only add the subclass to your testing target.

like image 121
Sheamus Avatar answered Sep 27 '22 18:09

Sheamus