Skip to main content

Improve Unit Test with OCMock

Adding OCMock to your project:

  • Step 1: Download a release from the downloads page.
  • Step 2: Setup OCMock from the iOS page.
  • Step 3: Add an import to your unit tests.
#import <OCMock/OCMock.h>

Contents:

  • 1. Example classes

Let's assume we are writing a little application. Our application have two classes as ClassA and ClassB. ClassB inherit from ClassA. They have some methods as below:
  • 2. Expect-run-verify

This is the original approach to mocking. First the mock object is set up with expectations, then the code under test is run, and afterwards the expectations are verified. If an expected method has not been invoked, or has not been invoked with the right arguments, then an error is reported. As shown it is possible to use argument constraints in the expect statement. Strict mocks can be created for classes and protocols.
Example codes:

  • 3. Verify after running

Verifies that some method has been called by the code under test. If the method has not been invoked an error is reported. In Xcode and AppCode the error is reported on the line of the verify, for other test environments an exception is thrown.
Base on the example of "Expect-Run-Verify", I rewrite as below:

  • 4. Void-Method

You can use ClassMock, PartialMock for test void method.
  • 5. Return-Method

You can use ClassMock, PartialMock for simulate the value is returned.
  • 6. Block

You can use ClassMock, PartialMock, andDo for simulate the value is returned from a block.
  • 7. Delegate and DataSource

You can use ProtocolMock for simulate a delegate or a datasource. Simulate the datasource of TableView
  • 8. Notification (Observer)

You can use ObserverMock for test NotificationCenter.
  • 9. Exception

You can use StrictClassMock for test Exception.
  • 10. Advanced topics

Verifying in order:The mock can be told to verify that expected methods are called in the same order as the expectations are set up. As soon as a method is called that is not next on the “expected list” the mock will fail fast and throw an exception. Using reject method: Using expect and return: Test to open an url:
Tools: XCode 7, OCMock 3.0

Reference documents:

- OCMock
- Engineering
- StackOverflow

Comments

Popular posts from this blog

So sánh những framework hỗ trợ viết ứng dụng trên SmartPhone

Khi lập trình trên SmartPhone bạn không nhất thiết phải học những ngôn ngữ đặc thù trên từng loại hệ điều hành thì mới có thể lập trình được. Ví dụ như muốn lập trình trên iOS thì phải học ngôn ngữ Objective-C hay Swift, muốn lập trình được trên Android thì học ngôn ngữ Java, muốn lập trình trên WinPhone thì học ngôn ngữ C#. Hiện nay có rất nhiều những framework giúp đỡ cho các bạn rất nhiều khi các bạn muốn viết trên nhiều nền tảng smartphone bằng ngôn ngữ mà bạn yêu thích. Theo mình thấy thì hiện nay có 3 loại như: Native App, Hybrid Mobile App, Native Cross-Platform App. 1. Native App: là những ứng dụng sử dụng những framework và ngôn ngữ lập trình của hệ thống cung cấp sẵn. Ví dụ như bạn muốn lập trình iOS thì phải cài XCode, học ngôn ngữ Objective-C hay Swift, lập trình Android thì cài Android Studio và học ngôn ngữ Java. - Ưu điểm: Hiệu năng thực thi ứng dụng trên nền tảng nhanh và hiệu quả. Không bị phụ thuộc vào bên thứ 3. Khi phát hành ứng dụng trên những Mobile Store cũng dễ...

Hướng dẫn dùng Serverless sử dụng Lambda AWS

1. Lambda function là gì? AWS Lambda cho phép bạn chạy mã mà không cần cung cấp hay quản lý máy chủ. Bạn chỉ phải trả tiền cho thời gian xử lý thông tin đã sử dụng. Với Lambda, bạn có thể chạy mã cho gần như toàn bộ các loại ứng dụng hay dịch vụ backend – tất cả đều không cần quản trị. Chỉ cần tải đoạn mã của bạn lên và Lambda sẽ lo hết những gì cần làm để chạy và mở rộng mã của bạn với mức độ có sẵn cao. Bạn có thể thiết lập mã của bạn tự động kích hoạt từ các dịch vụ AWS khác, hoặc gọi trực tiếp từ bất cứ ứng dụng web hay di động nào. Chi phí chạy trên lambda function rẻ so với chi phí bạn mua 1 con server, duy trì và quản trị nó ( ví dụ như bạn phải xử lý bất đồng bộ những request, khi lượng user bạn tăng đột biến bạn phải có cơ chế auto scale, chứ không thì server bị sẽ bị treo, khi server bị treo bạn phải tự động khởi động lại sẽ mất thời gian,... ).

Hướng dẫn deploy 1 static web trên GitHub domain

Hiện tại GitHub đã hỗ trợ 1 tool gh-pages dùng để bạn publish 1 static web lên trên GitHub. Cái này rất tiện nếu bạn muốn làm những bản demo cho khách hàng xem và không muốn mua 1 con server riêng để deploy và mất phí duy trì. Bạn có thể xài account free của GitHub để làm việc này luôn. Để demo tool này cách xài như thế nào mình xin dùng 1 free template ' paper-dashboard-react ' bạn có thể dùng bất kỳ free template nào có sẵn trên mạng về xài và sửa chúng lại. Bạn mở terminal lên và di chuyển đến folder chứa file 'package.json', cài tool gh-pages theo chế độ development như sau: npm install gh-pages --save-dev Sau đó bạn mở file 'package.json' lên thêm giá trị 'homepage' và 2 đoạn script 'predeploy' và 'deploy' bằng gh-pages như hình sau: Bạn mở file public/index.html và sửa lại chỗ 'manifest': Sau khi sửa xong hết những chỗ này và giờ bạn muốn deploy thì chỉ cần chạy đoạn script sau: npm run deploy Khi bạn chạy lần đầu tiên ...