user1844142
user1844142

Reputation: 89

Unit test in IOS

I have a UIbutton in my app. When I click the button, it removes the last object in a NSMutableArray. For this, I want to write unit tests.Please any one give me your suggestion.

I use this code for knowing when a click on the UIButton was performed:

[viewControllerObject.backButton1 sendActionsForControlEvents:UIControlEventTouchUpInside];

Thanks, Ricky.

Upvotes: 3

Views: 408

Answers (3)

user23743
user23743

Reputation:

At a "unit" level there are two things you're testing:

  • does tapping the button send the action method?
  • does the action method remove the last object from an array?

Ignore the first one, that's Apple's problem (or charitably it's an integration test). The second is straightforward if you think about the Assemble, Act, Assert process:

  1. Assemble: build your view controller and its content array.
  2. Act: call the action method.
  3. Assert: check that the last object was removed.
-(void)testRemovalOfLastObjectOnButtonAction
{
    //... build and populate the view controller
    id lastObject = [array lastObject];
    [viewController buttonTapped: sender];
    STAssertFalse([array containsObject: lastObject], @"Object %@ should be removed", lastObject);       
}

Note I test explicitly whether the last object was removed, not whether the count was decremented: that could happen if any object were removed.

Upvotes: 3

Bob Vork
Bob Vork

Reputation: 2947

XCode has native support for Unit Tests. If you start a new project, look for the check mark that says 'Include Unit Test'. If you use that, you will have a folder called <project_name>Tests. Open the .m file in there, and you'll see a - (void)testExample method where you can put your tests.

You can use a number of functions to test, like STAssertTrue and STAssertNotNil. Check out the Apple docs here: https://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/UnitTesting/03-Writing_Test_Case_Methods/writing_tests.html#//apple_ref/doc/uid/TP40002143-CH4-SW1

In your case, you could probably do something like this:

NSInteger arrayCount = mArray.count;
[yourInstance performButtonAction];
STAssertEquals(arrayCount -1, mArray.count);

Upvotes: 1

Mikael
Mikael

Reputation: 3612

You can do this in several different ways. A suggestion would be to watch this great tutorial

The video explains how to unit test in UIKit.

Upvotes: 1

Related Questions