title: Swift 3 Access dictionary (json) values from enum
body: <p>I'm trying to use an enum as described in this post <a href="https://8thlight.com/blog/mike-knepper/2017/01/30/keeping-third-party-swift-dependencies-testable.html" rel="nofollow noreferrer">https://8thlight.com/blog/mike-knepper/2017/01/30/keeping-third-party-swift-dependencies-testable.html</a> and am having a bit of trouble understanding how to use the enum return as my response. I've setup my code for a successful test (removed the fail and error cases for now) as:</p>
  
  <p>the enum response return definitions:</p>
  
  <pre><code>public struct Constants {
      public typealias jsonData = [String: Any]
      public typealias jsonErrors = [[String: Any]]
  }
  public enum ServiceResponse {
      case success(Constants.jsonData)
      case errors(Constants.jsonErrors)
      case failure(String)
  }
  </code></pre>
  
  <p>My mock response as:</p>
  
  <pre><code>private let successData: Constants.jsonData = [
      "user_email": "tester@ci.com",
      "user_display_name": "tester smith",
      "user_avatar_url": "",
      "user_id": 1
  ]
  
  private let successfulResponse = ServiceResponse.success(successData)
  </code></pre>
  
  <p>And the mock adapter (used for my test) as:</p>
  
  <pre><code>class MockSuccessfulNetworkAdapter: NetworkAdapter {
      func post(destination: String, payload: [String: Any], responseHandler: @escaping (ServiceResponse) -&gt; ()) {
          responseHandler(successfulResponse)
      }
  }
  </code></pre>
  
  <p>My service being tested is:</p>
  
  <pre><code>    public func validateUser(_ userData: [String: Any]?, completed: ((_ response: ServiceResponse) -&gt; ())? ) {
  
          let loginEndpoint = "\(Constants.BASE_ENDPOINT)\(Constants.LOGIN_ENDPOINT)"
          if let userPayload = userData {
  
              networkAdapter.post(destination: loginEndpoint, payload: userPayload, responseHandler: { (response) -&gt; Void in
                  print("User validate call response was \(response)")               
                  completed?(response)
              })
          }
          else {
              print( "Invalid User attempt to write JSON")
              return
          }
      }
  </code></pre>
  
  <p>And the test (with some of the avenues I've tried):</p>
  
  <pre><code>func testValidateAValidUser() {
      let networkAdapter = MockSuccessfulNetworkAdapter()
  
      if  let loginService = LoginService(Constants.BASE_ENDPOINT, networkAdapter) {
          let userData = [
              "email": "tester@a.com",
              "password": "password"
          ]
  
          loginService.validateUser(userData, completed: { (response: ServiceResponse?) -&gt; Void in
              print(response)
  
                  // case ServiceResponse.success (let data) {
  
                      print("Response for validate user -&gt; \(response!)")
  //                    var data = response? as! Constants.jsonData
  //                    if (response == ServiceResponse.success(data)) {
  //                        // let em = response ["email"]
  //                    }
                  // XCTAssertTrue(response == ServiceResponse.success)
                  // if let em = response ["email"] {
                      print("OK!!")
                  // }
                  // XCTAssertTrue( , "tester@ci.com")
              // }
              })
          }
      }    
  </code></pre>
  
  <p>I like the idea of utilizing the enums and the dependency injection for my testing, but can't seem to wrangle getting the actual values (as I will have to when I move to the alamofire or something to decode the actual values. Any help greatly appreciated!</p>
  
  <p>Edit: add response</p>
  
  <p>What I'm seeing as my response is correct when running the test:
  <a href="https://i.stack.imgur.com/LcGqb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LcGqb.png" alt="enter image description here"></a></p>