ios behavior-driven development
DESCRIPTION
Behavioral-driven development of a sample app using Kiwi and Nocilla.TRANSCRIPT
iOS Behavior-Driven DevelopmentTesting RESTful Applications with Kiwi and Nocilla
Brian Gesiak
March 9th, 2014
Research Student, The University of Tokyo
@modocache #startup_ios
Today
• Behavior-driven development (BDD) • iOS behavior-driven development
• Kiwi • Testing asynchronous networking
• Nocilla
Test-Driving Network Code
• Let’s say we want to display a user’s repositories on GitHub
• We can GET JSON from the GitHub API
• https://api.github.com/users/{{ username }}/repos.json
Motivation
Test-Driving Network CodeMotivation
/// GET /users/:username/repos ![ { "id": 1296269, "name": "Hello-World", "description": "My first repo!", /* ... */ } ]
Test-Driving Network CodeDemonstration
Building the AppBehavior-Driven Development Using Kiwi
• Behavior-driven development (BDD) is an extension of test-driven development
Test-Driven Development
Test-Driven Development
• Red: Write a test and watch it fail
Test-Driven Development
• Red: Write a test and watch it fail• Green: Pass the test (by writing as little code as possible)
Test-Driven Development
• Red: Write a test and watch it fail• Green: Pass the test (by writing as little code as possible)• Refactor: Remove duplication
Test-Driven Development
• Red: Write a test and watch it fail• Green: Pass the test (by writing as little code as possible)• Refactor: Remove duplication• Repeat
Example of iOS TDD Using XCTest
// Create an XCTestCase subclass @interface GHVRepoCollectionTests : XCTestCase // The subject of our tests @property (nonatomic, strong) GHVCollection *collection; @end !@implementation GHVRepoCollectionTests // Set up the `repos` collection for each test - (void)setUp { [super setUp]; self.collection = [GHVCollection new]; } // Add a test - (void)testAddingARepo { // Add a repo [self.collection addRepo:[GHVRepo new]]; ! // Assert the number of repos is now one XCTAssertEqual([self.collection.repos count], 1, @"Expected one repository"); } @end
Example of iOS TDD Using XCTest
// Create an XCTestCase subclass @interface GHVRepoCollectionTests : XCTestCase // The subject of our tests @property (nonatomic, strong) GHVCollection *collection; @end !@implementation GHVRepoCollectionTests // Set up the `repos` collection for each test - (void)setUp { [super setUp]; self.collection = [GHVCollection new]; } // Add a test - (void)testAddingARepo { // Add a repo [self.collection addRepo:[GHVRepo new]]; ! // Assert the number of repos is now one XCTAssertEqual([self.collection.repos count], 1, @"Expected one repository"); } @end
Example of iOS TDD Using XCTest
// Create an XCTestCase subclass @interface GHVRepoCollectionTests : XCTestCase // The subject of our tests @property (nonatomic, strong) GHVCollection *collection; @end !@implementation GHVRepoCollectionTests // Set up the `repos` collection for each test - (void)setUp { [super setUp]; self.collection = [GHVCollection new]; } // Add a test - (void)testAddingARepo { // Add a repo [self.collection addRepo:[GHVRepo new]]; ! // Assert the number of repos is now one XCTAssertEqual([self.collection.repos count], 1, @"Expected one repository"); } @end
Example of iOS TDD Using XCTest
// Create an XCTestCase subclass @interface GHVRepoCollectionTests : XCTestCase // The subject of our tests @property (nonatomic, strong) GHVCollection *collection; @end !@implementation GHVRepoCollectionTests // Set up the `repos` collection for each test - (void)setUp { [super setUp]; self.collection = [GHVCollection new]; } // Add a test - (void)testAddingARepo { // Add a repo [self.collection addRepo:[GHVRepo new]]; ! // Assert the number of repos is now one XCTAssertEqual([self.collection.repos count], 1, @"Expected one repository"); } @end
Example of iOS TDD Using XCTest
// Create an XCTestCase subclass @interface GHVRepoCollectionTests : XCTestCase // The subject of our tests @property (nonatomic, strong) GHVCollection *collection; @end !@implementation GHVRepoCollectionTests // Set up the `repos` collection for each test - (void)setUp { [super setUp]; self.collection = [GHVCollection new]; } // Add a test - (void)testAddingARepo { // Add a repo [self.collection addRepo:[GHVRepo new]]; ! // Assert the number of repos is now one XCTAssertEqual([self.collection.repos count], 1, @"Expected one repository"); } @end
Example of iOS TDD Using XCTest
// Create an XCTestCase subclass @interface GHVRepoCollectionTests : XCTestCase // The subject of our tests @property (nonatomic, strong) GHVCollection *collection; @end !@implementation GHVRepoCollectionTests // Set up the `repos` collection for each test - (void)setUp { [super setUp]; self.collection = [GHVCollection new]; } // Add a test - (void)testAddingARepo { // Add a repo [self.collection addRepo:[GHVRepo new]]; ! // Assert the number of repos is now one XCTAssertEqual([self.collection.repos count], 1, @"Expected one repository"); } @end
Example of iOS TDD Using XCTest
Behavior-Driven Development
• Answers the question: “What do I test?” • Behavioral tests don’t test the implementation, they specify the behavior
iOS BDD Using Kiwi
// Describe how the `GHVCollection` class behaves describe(@"GHVCollection", ^{ // Setup up the collection for each test __block GHVCollection *collection = nil; beforeEach(^{ collection = [GHVCollection new]; }); ! // Describe how the `-addRepo:` method behaves describe(@"-addRepo:", ^{ context(@"after adding a repo", ^{ // Add a repo before each test beforeEach(^{ [collection addRepo:[GHVRepo new]]; }); // Test the method behaves correctly it(@"has a repo count of one", ^{ [[collection.repos should] haveCountOf:1]; }); }); }); });
iOS BDD Using Kiwi
// Describe how the `GHVCollection` class behaves describe(@"GHVCollection", ^{ // Setup up the collection for each test __block GHVCollection *collection = nil; beforeEach(^{ collection = [GHVCollection new]; }); ! // Describe how the `-addRepo:` method behaves describe(@"-addRepo:", ^{ context(@"after adding a repo", ^{ // Add a repo before each test beforeEach(^{ [collection addRepo:[GHVRepo new]]; }); // Test the method behaves correctly it(@"has a repo count of one", ^{ [[collection.repos should] haveCountOf:1]; }); }); }); });
iOS BDD Using Kiwi
// Describe how the `GHVCollection` class behaves describe(@"GHVCollection", ^{ // Setup up the collection for each test __block GHVCollection *collection = nil; beforeEach(^{ collection = [GHVCollection new]; }); ! // Describe how the `-addRepo:` method behaves describe(@"-addRepo:", ^{ context(@"after adding a repo", ^{ // Add a repo before each test beforeEach(^{ [collection addRepo:[GHVRepo new]]; }); // Test the method behaves correctly it(@"has a repo count of one", ^{ [[collection.repos should] haveCountOf:1]; }); }); }); });
iOS BDD Using Kiwi
// Describe how the `GHVCollection` class behaves describe(@"GHVCollection", ^{ // Setup up the collection for each test __block GHVCollection *collection = nil; beforeEach(^{ collection = [GHVCollection new]; }); ! // Describe how the `-addRepo:` method behaves describe(@"-addRepo:", ^{ context(@"after adding a repo", ^{ // Add a repo before each test beforeEach(^{ [collection addRepo:[GHVRepo new]]; }); // Test the method behaves correctly it(@"has a repo count of one", ^{ [[collection.repos should] haveCountOf:1]; }); }); }); });
iOS BDD Using Kiwi
// Describe how the `GHVCollection` class behaves describe(@"GHVCollection", ^{ // Setup up the collection for each test __block GHVCollection *collection = nil; beforeEach(^{ collection = [GHVCollection new]; }); ! // Describe how the `-addRepo:` method behaves describe(@"-addRepo:", ^{ context(@"after adding a repo", ^{ // Add a repo before each test beforeEach(^{ [collection addRepo:[GHVRepo new]]; }); // Test the method behaves correctly it(@"has a repo count of one", ^{ [[collection.repos should] haveCountOf:1]; }); }); }); });
iOS BDD Using Kiwi
// Describe how the `GHVCollection` class behaves describe(@"GHVCollection", ^{ // Setup up the collection for each test __block GHVCollection *collection = nil; beforeEach(^{ collection = [GHVCollection new]; }); ! // Describe how the `-addRepo:` method behaves describe(@"-addRepo:", ^{ context(@"after adding a repo", ^{ // Add a repo before each test beforeEach(^{ [collection addRepo:[GHVRepo new]]; }); // Test the method behaves correctly it(@"has a repo count of one", ^{ [[collection.repos should] haveCountOf:1]; }); }); }); });
iOS BDD Using Kiwi
Kiwi Benefits
Kiwi Benefits
• An unlimited amount of setup and teardown
Kiwi Benefits
• An unlimited amount of setup and teardownbeforeEach(^{ /* ... */ }); beforeAll(^{ /* ... */ }); afterEach(^{ /* ... */ }); afterAll(^{ /* ... */ });
Kiwi Benefits
• An unlimited amount of setup and teardownbeforeEach(^{ /* ... */ }); beforeAll(^{ /* ... */ }); afterEach(^{ /* ... */ }); afterAll(^{ /* ... */ });
• Mocks and stubs included
Kiwi Benefits
• An unlimited amount of setup and teardownbeforeEach(^{ /* ... */ }); beforeAll(^{ /* ... */ }); afterEach(^{ /* ... */ }); afterAll(^{ /* ... */ });
• Mocks and stubs included[collection stub:@selector(addRepo:)];
Kiwi Benefits
• An unlimited amount of setup and teardownbeforeEach(^{ /* ... */ }); beforeAll(^{ /* ... */ }); afterEach(^{ /* ... */ }); afterAll(^{ /* ... */ });
• Mocks and stubs included[collection stub:@selector(addRepo:)];
• Asynchronous testing support
Kiwi Benefits
• An unlimited amount of setup and teardownbeforeEach(^{ /* ... */ }); beforeAll(^{ /* ... */ }); afterEach(^{ /* ... */ }); afterAll(^{ /* ... */ });
• Mocks and stubs included[collection stub:@selector(addRepo:)];
• Asynchronous testing support[[collection.repos shouldEventually] haveCountOf:2];
Kiwi Benefits
• An unlimited amount of setup and teardownbeforeEach(^{ /* ... */ }); beforeAll(^{ /* ... */ }); afterEach(^{ /* ... */ }); afterAll(^{ /* ... */ });
• Mocks and stubs included[collection stub:@selector(addRepo:)];
• Asynchronous testing support[[collection.repos shouldEventually] haveCountOf:2];
• More readable than XCTest
Our First Failing Test
/// GHVAPIClientSpec.m !it(@"gets repositories", ^{ // The repos returned by the API __block NSArray *allRepos = nil; ! // Fetch the repos from the API [client allRepositoriesForUsername:@"modocache" success:^(NSArray *repos) { // Set the repos allRepos = repos; } failure:nil]; ! // Assert that the repos have been set [[expectFutureValue(allRepos) shouldEventually] haveCountOf:10]; });
Our First Failing Test
/// GHVAPIClientSpec.m !it(@"gets repositories", ^{ // The repos returned by the API __block NSArray *allRepos = nil; ! // Fetch the repos from the API [client allRepositoriesForUsername:@"modocache" success:^(NSArray *repos) { // Set the repos allRepos = repos; } failure:nil]; ! // Assert that the repos have been set [[expectFutureValue(allRepos) shouldEventually] haveCountOf:10]; });
Our First Failing Test
/// GHVAPIClientSpec.m !it(@"gets repositories", ^{ // The repos returned by the API __block NSArray *allRepos = nil; ! // Fetch the repos from the API [client allRepositoriesForUsername:@"modocache" success:^(NSArray *repos) { // Set the repos allRepos = repos; } failure:nil]; ! // Assert that the repos have been set [[expectFutureValue(allRepos) shouldEventually] haveCountOf:10]; });
Our First Failing Test
/// GHVAPIClientSpec.m !it(@"gets repositories", ^{ // The repos returned by the API __block NSArray *allRepos = nil; ! // Fetch the repos from the API [client allRepositoriesForUsername:@"modocache" success:^(NSArray *repos) { // Set the repos allRepos = repos; } failure:nil]; ! // Assert that the repos have been set [[expectFutureValue(allRepos) shouldEventually] haveCountOf:10]; });
Our First Failing Test
/// GHVAPIClientSpec.m !it(@"gets repositories", ^{ // The repos returned by the API __block NSArray *allRepos = nil; ! // Fetch the repos from the API [client allRepositoriesForUsername:@"modocache" success:^(NSArray *repos) { // Set the repos allRepos = repos; } failure:nil]; ! // Assert that the repos have been set [[expectFutureValue(allRepos) shouldEventually] haveCountOf:10]; });
Our First Failing Test
/// GHVAPIClientSpec.m !it(@"gets repositories", ^{ // The repos returned by the API __block NSArray *allRepos = nil; ! // Fetch the repos from the API [client allRepositoriesForUsername:@"modocache" success:^(NSArray *repos) { // Set the repos allRepos = repos; } failure:nil]; ! // Assert that the repos have been set [[expectFutureValue(allRepos) shouldEventually] haveCountOf:10]; });
Our First Failing Test
Going Green
Going Green
/// GHVAPIClient.m !// Create a request operation manager pointing at the GitHub API NSString *urlString = @"https://api.github.com/"; NSURL *baseURL = [NSURL URLWithString:urlString]; AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseURL]; !// The manager should serialize the response as JSON manager.requestSerializer = [AFJSONRequestSerializer serializer]; !// Send a request to GET /users/:username/repos [manager GET:[NSString stringWithFormat:@"users/%@/repos", username] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { // Send response object to success block success(responseObject); } failure:nil]; }
Going Green
/// GHVAPIClient.m !// Create a request operation manager pointing at the GitHub API NSString *urlString = @"https://api.github.com/"; NSURL *baseURL = [NSURL URLWithString:urlString]; AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseURL]; !// The manager should serialize the response as JSON manager.requestSerializer = [AFJSONRequestSerializer serializer]; !// Send a request to GET /users/:username/repos [manager GET:[NSString stringWithFormat:@"users/%@/repos", username] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { // Send response object to success block success(responseObject); } failure:nil]; }
Going Green
/// GHVAPIClient.m !// Create a request operation manager pointing at the GitHub API NSString *urlString = @"https://api.github.com/"; NSURL *baseURL = [NSURL URLWithString:urlString]; AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseURL]; !// The manager should serialize the response as JSON manager.requestSerializer = [AFJSONRequestSerializer serializer]; !// Send a request to GET /users/:username/repos [manager GET:[NSString stringWithFormat:@"users/%@/repos", username] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { // Send response object to success block success(responseObject); } failure:nil]; }
Going Green
/// GHVAPIClient.m !// Create a request operation manager pointing at the GitHub API NSString *urlString = @"https://api.github.com/"; NSURL *baseURL = [NSURL URLWithString:urlString]; AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseURL]; !// The manager should serialize the response as JSON manager.requestSerializer = [AFJSONRequestSerializer serializer]; !// Send a request to GET /users/:username/repos [manager GET:[NSString stringWithFormat:@"users/%@/repos", username] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { // Send response object to success block success(responseObject); } failure:nil]; }
Going Green
/// GHVAPIClient.m !// Create a request operation manager pointing at the GitHub API NSString *urlString = @"https://api.github.com/"; NSURL *baseURL = [NSURL URLWithString:urlString]; AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseURL]; !// The manager should serialize the response as JSON manager.requestSerializer = [AFJSONRequestSerializer serializer]; !// Send a request to GET /users/:username/repos [manager GET:[NSString stringWithFormat:@"users/%@/repos", username] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { // Send response object to success block success(responseObject); } failure:nil]; }
Problems with our Test
• The test has external dependencies • It’ll fail if the GitHub API is down • It’ll fail if run without an internet connection • It’ll fail if the response is too slow
• The test is slow • It sends a request every time it’s run
HTTP Stubbing
stubRequest(@"GET", @“https://api.github.com/" @“users/modocache/repos") .andReturn(200) .withHeaders(@{@"Content-Type": @"application/json"}) .withBody(@"[\"repo-1\"]"); !GHVAPIClient *client = [GHVAPIClient new]; !// ... ![[expectFutureValue(allRepos) shouldEventually] haveCountOf:1];
Eliminating external dependencies
HTTP Stubbing
stubRequest(@"GET", @“https://api.github.com/" @“users/modocache/repos") .andReturn(200) .withHeaders(@{@"Content-Type": @"application/json"}) .withBody(@"[\"repo-1\"]"); !GHVAPIClient *client = [GHVAPIClient new]; !// ... ![[expectFutureValue(allRepos) shouldEventually] haveCountOf:1];
Eliminating external dependencies
HTTP Stubbing
stubRequest(@"GET", @“https://api.github.com/" @“users/modocache/repos") .andReturn(200) .withHeaders(@{@"Content-Type": @"application/json"}) .withBody(@"[\"repo-1\"]"); !GHVAPIClient *client = [GHVAPIClient new]; !// ... ![[expectFutureValue(allRepos) shouldEventually] haveCountOf:1];
Eliminating external dependencies
HTTP Stubbing
stubRequest(@"GET", @“https://api.github.com/" @“users/modocache/repos") .andReturn(200) .withHeaders(@{@"Content-Type": @"application/json"}) .withBody(@"[\"repo-1\"]"); !GHVAPIClient *client = [GHVAPIClient new]; !// ... ![[expectFutureValue(allRepos) shouldEventually] haveCountOf:1];
Eliminating external dependencies
HTTP Stubbing
stubRequest(@"GET", @“https://api.github.com/" @“users/modocache/repos") .andReturn(200) .withHeaders(@{@"Content-Type": @"application/json"}) .withBody(@"[\"repo-1\"]"); !GHVAPIClient *client = [GHVAPIClient new]; !// ... ![[expectFutureValue(allRepos) shouldEventually] haveCountOf:1];
Eliminating external dependencies
HTTP Stubbing
stubRequest(@"GET", @“https://api.github.com/" @“users/modocache/repos") .andReturn(200) .withHeaders(@{@"Content-Type": @"application/json"}) .withBody(@"[\"repo-1\"]"); !GHVAPIClient *client = [GHVAPIClient new]; !// ... ![[expectFutureValue(allRepos) shouldEventually] haveCountOf:1];
Eliminating external dependencies
Problems Nocilla Fixes
• The test no longer has external dependencies • It’ll pass whether the GitHub API is online or not • It’ll pass even when run offline
• The test is fast • It still sends a request, but that request is immediately intercepted and a response is returned
Other Nocilla Features
Other Nocilla Features
• Stub HTTP requests using regular expressions
Other Nocilla Features
• Stub HTTP requests using regular expressionsstubRequest(@"GET", @"https://api.github.com/" @"users/(.*?)/repos".regex)
Other Nocilla Features
• Stub HTTP requests using regular expressionsstubRequest(@"GET", @"https://api.github.com/" @"users/(.*?)/repos".regex)
• Return errors, such as for poor internet connection
Other Nocilla Features
• Stub HTTP requests using regular expressionsstubRequest(@"GET", @"https://api.github.com/" @"users/(.*?)/repos".regex)
• Return errors, such as for poor internet connectionNSError *error = [NSError errorWithDomain:NSURLErrorDomain code:29 userInfo:@{NSLocalizedDescriptionKey: @"Uh-oh!"}]; stubRequest(@"GET", @"...") .andFailWithError(error);
Takeaways
Takeaways
• Readable, behavior-driven, asynchronous tests with Kiwi • https://github.com/allending/Kiwi
Takeaways
• Readable, behavior-driven, asynchronous tests with Kiwi • https://github.com/allending/Kiwi
pod "Kiwi/XCTest"
Takeaways
• Readable, behavior-driven, asynchronous tests with Kiwi • https://github.com/allending/Kiwi
• Eliminate network dependencies with Nocilla • https://github.com/luisobo/Nocilla
pod "Kiwi/XCTest"
Takeaways
• Readable, behavior-driven, asynchronous tests with Kiwi • https://github.com/allending/Kiwi
• Eliminate network dependencies with Nocilla • https://github.com/luisobo/Nocilla
pod "Kiwi/XCTest"
pod "Nocilla"
Questions?@modocache #startup_ios
describe(@"this talk", ^{ context(@"after presenting the slides", ^{ it(@"moves to Q&A", ^{ [[you should] askQuestions]; [[you shouldEventually] receive:@selector(stop)]; }); }); });
Questions?@modocache #startup_ios
describe(@"this talk", ^{ context(@"after presenting the slides", ^{ it(@"moves to Q&A", ^{ [[you should] askQuestions]; [[you shouldEventually] receive:@selector(stop)]; }); }); });
Questions?@modocache #startup_ios
describe(@"this talk", ^{ context(@"after presenting the slides", ^{ it(@"moves to Q&A", ^{ [[you should] askQuestions]; [[you shouldEventually] receive:@selector(stop)]; }); }); });
Questions?@modocache #startup_ios
describe(@"this talk", ^{ context(@"after presenting the slides", ^{ it(@"moves to Q&A", ^{ [[you should] askQuestions]; [[you shouldEventually] receive:@selector(stop)]; }); }); });
Questions?@modocache #startup_ios
describe(@"this talk", ^{ context(@"after presenting the slides", ^{ it(@"moves to Q&A", ^{ [[you should] askQuestions]; [[you shouldEventually] receive:@selector(stop)]; }); }); });
Questions?@modocache #startup_ios