By Rupert
mac
iPhone Note #11: Unit Testing
Sep 7th
Test first, develop later! That’s the greeting when you visit OCUnit, similar to JUnit. Note that for this tutorial, you do not need to install OCUnit as it comes “built-in” in XCode as of v2.1.
1. Create a new iPhone Window-based application project “SampleTest”.
2. Our subject for testing is Converter.m which converts kilometers to meters. Let’s implement an incorrect conversion by specifying 1km = 100 meters (should be 1000 meters) so we can see that the unit test captures it below…
#import "Converter.h" @implementation Converter - (id)init{ if(self = [super init]){ } return self; } - (int)convertKilometersToMeters:(int)km{ return km * 100; } @end
3. Add another target “UnitTests”. Right click on Targets -> Add -> New Target… -> Choose Unit Test Bundle.

4. Name it “UnitTests”. After hitting submit, you will be presented with the project settings for “UnitTests”.

5. Go to the General Tab -> Click on the “+” icon above “Linked Libraries”. Choose “SampleTest” as the application we have direct dependency with.

6. Close the Settings. To check, navigate under “Groups & Files” -> Targets. You should see the SampleTest Application Icon just below “UnitTests”.

7. Right Click on “Sample Test” -> Add -> New File…

8. Name the file “ConverterTest. Don’t forget to also create the header file (default). Specify it also in a different directory under “Location”. Then check the UnitTests as the “Targets”. When you hit “Finish” it will ask you to create the folder “Tests”

Tip: Keep things organize and put it under a “Tests” Group. Right Click on “Sample Test” -> Add -> New Group… Name it “Tests”, then drag the files (ConverterTest.h and ConverterTest.m) into that group.
9. Open up ConverterTest.h and notice that “SenTestingKit.h” is already imported. Now let’s add method testKilometersToMeters as shown below. Test methods usually start out with a test prefix.
In the implementation, let’s import Converter.h and use STAssertTrue. To test the convertKilometersToMeters method, we are asserting that the result should be 1000. If not, then we should know! That is why we are writing a unit test for.. making sure that our implementation doesn’t break.
#import "ConverterTest.h" #import "Converter.h" @implementation ConverterTest - (void)testKilometersToMeters{ int km = 1; Converter *converter = [[Converter alloc] init]; int meters = [converter convertKilometersToMeters:km]; STAssertTrue(meters == 1000, @"converting %d km to meters should equal 1000, instead received %d", km, meters); } @end
12. Now, before we build our target “UnitTests”, we need to include additional class references from our application. Drag Converter.m to the “Compile Sources” under UnitTests.

13. Now we can build. There are many ways to do this. My preference is to do a clean build when testing. Right Click on Sample Test then choose “Clean SampleTest”. Afterwards choose “Build SampleTest”.

If you have a succesful build for SampleTest, lets do the same for our “UnitTests”.

13. Here’s the crux of it. Notice the error in your “Build Results”?
/Users/rupert/projects/iphone/SampleTest/Tests/ConverterTest.m:18: error: -[ConverterTest testKilometersToMeters] : "meters == 1000" should be true. converting 1 km to meters should equal 1000, instead received 100

Now changing the correct implementation of convertKilometersToMeters will put the error away and you will have a successful build.
- (int)convertKilometersToMeters:(int)km{ return km * 1000; }
14. Look up the assert methods from SenTest.h.
#import <Foundation/NSObject.h> #import "SenTest.h" #define STAssertNil(a1, description, ...) #define STAssertNotNil(a1, description, ...) #define STAssertTrue(expression, description, ...) #define STAssertFalse(expression, description, ...) #define STAssertEqualObjects(a1, a2, description, ...) #define STAssertEquals(a1, a2, description, ...) #define STAssertEqualsWithAccuracy(left, right, accuracy, description, ...) #define STAssertFalseNoThrow(expression, description, ...) ....
References:
http://developer.apple.com/tools/unittest.html
Download: SampleTest.zip
Mac Softwares and CheatSheet
Aug 26th
1. iTerm – terminal with tabs.
2. MarsEdit- Blog Software. Im doing a local post on a local wordpress then copying and pasting to a remote wordpress.
3. Mac Shortcuts from http://www.danrodney.com/mac/index.html. Here’s a local post and another one.
4. How to create an ISO?
hdiutil makehybrid -o CS3v1.iso CS3
5. chmOX – CHM Viewer in OSX.
6. Git for OS X from google code.
7. Open a finder from terminal
open .
8. Keychain Access asking on passwordless ssh?
ssh-add Enter passphrase: ****** ssh-add -l <To list your identities>
Firefox crashes on my Mac caused by Java Applet VM
Dec 17th
After manually updating to Java MacOSX10.5 Update 2 two months ago, I noticed something weird with my Firefox. At first, I ignored it, “heh must be a screwed-up page Im trying to open…” But lately, I noticed that most of the pages I am opening with Java applets crashes consistently. So here is the culprit, try to delete java caches if there are any…
Trying to figure out which version of the java applet being used by FF


Deleting the cache.. Go to
/Application/Utilities/Java/ and launch Java Preferences.app or you could seach for “java preferences” in spotlight.

If the problem still persist, I bet we can trim down which java versions to use…

Rails Note #12: Oracle on Intel Mac
Dec 5th
2. Install Oracle Instant Client on Mac.
a. Instant Client Package – Basic: All files required to run OCI, OCCI, and JDBC-OCI applications
- instantclient-basic-macosx-10.2.0.4.0.zip (34,020,719 bytes)
b. *Instant Client Package – SDK: Additional header files and an example makefile for developing Oracle applications with Instant Client
instantclient-sdk-macosx-10.2.0.4.0.zip (603,493 bytes)
OR download the whole bundle (10.2.0.4.zip) with sqlplus installed from my installers.
3. Put this on your sudo vim ~/.bash_profile.
export ORACLE_HOME=/Library/Oracle/instantclient/10.2.0.4 export TNS_ADMIN=$ORACLE_HOME export LD_LIBRARY_PATH=$ORACLE_HOME export DYLD_LIBRARY_PATH=$ORACLE_HOME export PATH=$PATH:$ORACLE_HOME
4. Make a symbolic link
cd /Library/Oracle/instantclient/10.2.0.4 ln -s libclntsh.dylib.10.1 libclntsh.dylib
5. Go to /Library/Oracle/instantclient/10.2.0.4 and edit tnsnames.ora. Point the Oracle SID to the IP where you installed Oracle.
ORCL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.155)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
ORCL_2_11 =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.2.11)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)6. Install the oracle-adapter for rails
sudo gem install activerecord-oracle-adapter --source http://gems.rubyonrails.org
7. In your database.yml file
development: adapter: oracle database: orcl username: youzhu_mobile_dev password: your_password
or browse the contents of a sample rails project youzhumobile.tar.gz
8. If you ever encounter an encoding problem, then we need to set the NLS_LANG environment variable before running script/server.
# export NLS_LANG=American_America.UTF8 # script/server
or I prefer setting it in the environment.rb
Rails::Initializer.run do |config| ENV['NLS_LANG']='American_America.UTF8' # Settings in config/environments/* take precedence over those specified here.
Note: If you don’t know your database encoding, then read this post.
Mac Tip Of the Day: Know your keyboard
Nov 6th
1. Thanks to Chester for this one…

2. You can drag an image from a browser right to your desktop. This is faster than doing [Command] + [SHIFT] + F4 then cropping the picture out of the webpage..
Comments