Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6835505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:14:26+00:00 2026-05-26T23:14:26+00:00

I have a method that triggers an asynchronous request in the model and passes

  • 0

I have a method that triggers an asynchronous request in the model and passes a block that handles the response:

[user loginWithEmail:self.eMailTextField.text
         andPassword:self.passwordTextField.text
               block:^(UserLoginResponse response) {
                   switch (response) {
                       case UserLoginResponseSuccess:
                       {
                           // hooray
                           break;
                       }
                       case UserLoginResponseFailureAuthentication:
                           // bad credentials
                           break;
                       case UserLoginResponseFailureB:
                           // failure reason b
                           break;
                       default:
                           // unknown error
                           break;
                   }
               }];

The called method sets some parameters for the request and uses AFNetworking to start it.

Now I want to write a unit test to make sure the calling class reacts properly for every possible UserLoginResponse. I’m using Kiwi for testing but I think this is more of a general question…

How would I mock the argument that gets passed to the block from the user object?
The only way I can think of is to mock the underlying request and return the status code I expect for the test. Is there a better way?

It would also be possible to replace the block by using a delegate but I would definitely prefer using blocks here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:14:27+00:00Added an answer on May 26, 2026 at 11:14 pm

    It seems like there are 2 distinct things you want to verify here: 1) the user object passes the actual response to the block, and 2) the block handles various response codes appropriately.

    For #1, it seems like the right approach is to mock the request (the example uses OCMock and Expecta syntax):

    [[[request expect] andReturn:UserLoginResponseSuccess] authenticate];
    
    __block UserLoginResponse actual;
    
    [user loginWithEmail:nil
             andPassword:nil
                   block:^(UserLoginResponse expected) {
                       actual = expected;
                   }];
    
    expect(actual).toEqual(UserLoginResponseSuccess);
    

    For #2, I’d create a method that returns the block you want to validate. Then you can test it directly without all the other dependencies:

    In your header:

    typedef void(^AuthenticationHandlerBlock)(UserLoginResponse);
    -(AuthenticationHandlerBlock)authenticationHandler;
    

    In your implementation:

    -(AuthenticationHandlerBlock)authenticationHandler {
        return ^(UserLoginResponse response) {
           switch (response) {
               case UserLoginResponseSuccess:
               {
                   // hooray
                   break;
               }
               case UserLoginResponseFailureAuthentication:
                   // bad credentials
                   break;
               case UserLoginResponseFailureB:
                   // failure reason b
                   break;
               default:
                   // unknown error
                   break;
           }
        }
    }
    

    In your test:

    AuthenticationHandlerBlock block = [user authenticationHandler];
    block(UserLoginResponseSuccess);
    // verify success outcome
    block(UserLoginResponseFailureAuthentication);
    // verify failure outcome
    block(UserLoginResponseFailureB);
    // verify failure B outcome
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method that where I want to redirect the user back to
I have an onClick method set in the xml layout file that triggers the
I have a c++ class that triggers some method like an event. class Blah
I have a view controller that has a button that triggers a method to
I have a CADisplayLink that triggers a draw method in a Director object. I
I have class method that returns a list of employees that I can iterate
I have a method that can return either a single object or a collection
I have a method that takes an IQueryable. Is there a LINQ query that
I have a method that periodically (e.g. once in every 10 secs) try to
I have a method that's about ten lines of code. I want to create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.