I'm developing a framework in iOS which makes HTTP calls to server.I wanted to write unit test to test the API's.I wanted to mock the server calls,without actually making real server call.Can anyone help me with sample unit test which makes mocked server calls.How to do we set expectations and return the hand constructed url response.?I'm using XCTest framework for unit test and OCMock for mocking objects.
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- Dependencies while implementing Mocking in Junit4
相关文章
- 现在使用swift开发ios应用好还是swift?
- How to replace file-access references for a module
- How to mock methods return object with deleted cop
- What is a good way of cleaning up after a unit tes
-
EF6 DbSet
returns null in Moq - UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
Here is how you might mock sendAsynchronousRequest:
EDIT
To add clarification, here's what's happening. In this example our real code is calling
[NSURLConnection sendAsynchronousRequest: queue: completionHandler:]
There are three arguments passed to this method, plus the additional_cmd
andself
that is passed to all Objective-C method calls. Thus when we examine ourNSInvocation
the argument at index 4 is our completion handler. Since we are simulating the response from the server, we want to call this handler with fake data that represents the case we are testing.The signature for the method call
getArgumentAtIndexAsObject
is in a header file included with OCMock, but is not included by default in the framework. I believe it is calledNSInvocation+OCMAdditions.h
. It makes fetching the invocation arguments easier by returning anid
.Use OHTTPStubs, https://github.com/AliSoftware/OHHTTPStubs.
Here's a practical example of mocking the response to a specific GET request and returning JSON data.