IOS Unit Test Ocmock Introduction

Source: Internet
Author: User
Tags prepare stub
Ocmock SummaryThe use of Ocmock is for certain objects that are not easily constructed or acquired, and are created for testing with a virtual object. The Cocoa programming test uses a system-led ocunit, or a third-party ghunit without a mock, typically using a combination of OC mocks and ghunit to build a test framework to test.
The basic usage of mocks is to initialize mock objects from class,
ID mocks = [Ocmockobject Mockforclass:[someclass class]]
After you initialize a mock object, you can use the expect and verify combinations to authenticate.
[[Mock expect] somemethod:someargument]
The middle call to other code will call the SomeMethod
[Mock Verify]
Expect can also validate return values, such as
[[[[[Mock expect] andreturn:@ "objective-c"] lowercasestring];

Stub constructs an interface that returns information, but does not validate that the difference between expect and stub implementations is that there is a expectations array on the mockobject, and after that, the stub object is added to the array, verify Time to validate the expectations array.
[[[[Mock stub] andreturn:avalue] somemethod:someargument]
Tells mock object to return Avalue when SomeMethod calls
You can also throw an exception
[[[[Mock stub] andthrow:anexception] somemethod:someargument]
or send a message
[[[[Mock stub] andpost:anotification] somemethod:someargument]
or delegate another object
[[[Mock Stub] Andcall: @selector (amethod:) Onobject:anobject] somemethod:someargument]
You can use the and return, and Throw, and Post, and the call Onobject combination

In addition to getting mock objects from class, you can get them from protocol,
ID amock = [ocmockobject mockforprotocol: @protocol (Someprotocol)]
There is another kind of partial mocks
ID amock = [ocmockobject Partialmockforobject:anobject]
After the method is invoked without using a stub, it is returned to AnObject, which uses a anobject reference when a stub's method is invoked.
When a method of mock object is invoked without expect or stubs, an exception is generated, and if the Nicemock is used to initialize the initialization is ignored.
ID mocks = [Ocmockobject Nicemockforclass:[someclass class]]
You may not allow the specified method:
[[Mock Reject] somemethod]

Ocmock Documents: http://ocmock.org/

Ghunit Summary
Compared with the ocunit,ghunit can be compatible with Ocunit syntax, and provides an asynchronous test method. Test target is a stand-alone app that can be added to breakpoints and can be run on a real machine.
Using the Ghasynctestcase asynchronous test
For example
-(void) Testthatdelegatecanoverrideactionsheetdisplay {
Self.actionBar.delegate = self;

[Self prepare];
[Self.actionbar Sharebuttontouched:nil];
[Self waitforstatus:kghunitwaitstatussuccess timeout:1.0];
}

-(void) Actionbar: (Socializeactionbar *) Actionbar wantsdisplayactionsheet: (Uiactionsheet *) Actionsheet {
Ghassertequals (Actionsheet, Self.mockshareactionsheet, @ "bad action sheet");
[Self notify:kghunitwaitstatussuccess];
}
Call prepare first, call the method, and wait for the kghunitwaitstatussuccess message. Realize the Actionbar:wantsdisplayactionsheet: Delegate method, send kghunitwaitstatussuccess.

Ghunit Documents: Https://github.com/gabriel/gh-unit


Kif Summary
Kif, "Keep it Functional", allows iOS to do integration testing by simulating user interaction on the emulator.
Simulate user behavior including, input text, click, etc., verify the way can be used to jump to the specified view and so on.

The way to do this is to first copy the original target, introduce the library file, modify the configuration, and add the integration test code. The test code typically requires three parts, inherits from the Kiftestcontroller class, extends the Kiftestscenario method, specifies which steps to perform, each step calls the Kifteststep, Extend your kifteststep to achieve the specific actions of each step.

KIF Documents: Https://github.com/square/KIF


Another test framework Kiwi
Kiwi is similar to the system's own ocunit, adding test cases to the target of the system's own test.
In addition to the syntax differences, you can use mock objects as opposed to Ocunit, which can be tested asynchronously.
However, compared with Ghunit, the asynchronous test method is not easy to use.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.