<commit_before># DHURLSessionStub

[![CI Status](http://img.shields.io/travis/<NAME>/DHURLSessionStub.svg?style=flat)](https://travis-ci.org/<NAME>/DHURLSessionStub)
[![Version](https://img.shields.io/cocoapods/v/DHURLSessionStub.svg?style=flat)](http://cocoapods.org/pods/DHURLSessionStub)
[![License](https://img.shields.io/cocoapods/l/DHURLSessionStub.svg?style=flat)](http://cocoapods.org/pods/DHURLSessionStub)
[![Platform](https://img.shields.io/cocoapods/p/DHURLSessionStub.svg?style=flat)](http://cocoapods.org/pods/DHURLSessionStub)

## Usage

To run the example project, clone the repo, and run `pod install` from the Example directory first.

## Requirements

## Installation

DHURLSessionStub is available through [CocoaPods](http://cocoapods.org). To install
it, simply add the following line to your Podfile:

```ruby
pod "DHURLSessionStub"
```

## Author

<NAME>, <EMAIL>

## License

DHURLSessionStub is available under the MIT license. See the LICENSE file for more info.<commit_msg>Improved Readme<commit_after># DHURLSessionStub

[![CI Status](http://img.shields.io/travis/Dom<NAME>/DHURLSessionStub.svg?style=flat)](https://travis-ci.org/<NAME>/DHURLSessionStub)
[![Version](https://img.shields.io/cocoapods/v/DHURLSessionStub.svg?style=flat)](http://cocoapods.org/pods/DHURLSessionStub)
[![License](https://img.shields.io/cocoapods/l/DHURLSessionStub.svg?style=flat)](http://cocoapods.org/pods/DHURLSessionStub)
[![Platform](https://img.shields.io/cocoapods/p/DHURLSessionStub.svg?style=flat)](http://cocoapods.org/pods/DHURLSessionStub)

## Usage

To run the example project, clone the repo, and run `pod install` from the Example directory first.

## Stubbing NSURLSessions Data Task

1. Import the DHURLSessionStub (`import DHURLSessionStub`) in the file where you want to use `NSURLSession`. (In the example project, this is `ApiClient.swift`.
2. Use a lazy variable for the session to allow dependency injection in the test: `lazy var session: DHURLSession = NSURLSession.sharedSession()`.
3. Use `dataTaskWithURL` and `dataTaskWithRequest` as you always did.

Then you can stub your data tasks in your tests by assigning a URLSessionMock to the lazy session property you defined in step 2:

```swift
func testFetchingProfile_ReturnsPopulatedUser() {
  // Arrage
  let responseString = "{\"login\": \"dasdom\", \"id\": 1234567}"
  let responseData = responseString.dataUsingEncoding(NSUTF8StringEncoding)!
  let sessionMock = URLSessionMock(data: responseData, response: nil, error: nil)
  apiClient.session = sessionMock
  
  // Act
  apiClient.fetchProfileWithName("dasdom")
  
  // Assert
  let user = apiClient.user
  let expectedUser = User(name: "dasdom", id: 1234567)
  XCTAssertEqual(user, expectedUser)
}
```

Have a look at the example project for a complete picture how this works.

## How it works

It's qite easy. The URLSessionMock captures the completion handler of the data task. When the production code calls resume on the data task, the mock then calls the completion handler with the parameters given in the initializer.

## Requirements

iOS 8.3

## Installation

DHURLSessionStub is available through [CocoaPods](http://cocoapods.org). To install
it, simply add the following line to your Podfile:

```ruby
pod "DHURLSessionStub"
```

Alternatively, download [DHURLSessionMock](https://github.com/dasdom/DHURLSessionStub/blob/master/Pod/Classes/DHURLSessionMock.swift) and put it into your app target (not the test target!).

## Author

<NAME>, <EMAIL>

## License

DHURLSessionStub is available under the MIT license. See the LICENSE file for more info.
{'fraction_non_alphanumeric': 0.12215435868961688, 'fraction_numerical': 0.005830094392004442, 'mean_word_length': 5.411032028469751, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 9, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '548713', 'n_tokens_mistral': 1219, 'n_tokens_neox': 1184, 'n_words': 314}