I am trying to use the unit test method, which has a dependency on another class. The method calls the class method for this class, basically this:
func myMethod() {
Using the dependency injection method, I would like to be able to replace βTheirClassβ with the layout, but I cannot figure out how to do this. Is there a way to go in the mock class (and not in the instance)?
EDIT: Thanks for the answers. Perhaps I should have presented in more detail. The class method I'm trying to make fun of is in an open source library.
Below is my method. I am trying to test it by mocking the call to NXOAuth2Request.performMethod . This class method issues a network call to retrieve authenticated user information from our backend. In closing, I store this information in the global account repository provided by the open source library and post notifications of successes or failures.
func getUserProfileAndVerifyUserIsAuthenticated() { //this notification is fired when the refresh token has expired, and as a result, a new access token cannot be obtained NSNotificationCenter.defaultCenter().addObserver(self, selector: "didFailToGetAccessTokenNotification", name: NXOAuth2AccountDidFailToGetAccessTokenNotification, object: nil) let accounts = self.accountStore.accountsWithAccountType(UserAuthenticationScheme.sharedInstance.accountType) as Array<NXOAuth2Account> if accounts.count > 0 { let account = accounts[0] let userInfoURL = UserAuthenticationScheme.sharedInstance.userInfoURL println("getUserProfileAndVerifyUserIsAuthenticated: calling to see if user token is still valid") NXOAuth2Request.performMethod("GET", onResource: userInfoURL, usingParameters: nil, withAccount: account, sendProgressHandler: nil, responseHandler: { (response, responseData, error) -> Void in if error != nil { println("User Info Error: %@", error.localizedDescription); NSNotificationCenter.defaultCenter().postNotificationName("UserCouldNotBeAuthenticated", object: self) } else if let data = responseData { var errorPointer: NSError? let userInfo = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &errorPointer) as NSDictionary println("Retrieved user info") account.userData = userInfo NSNotificationCenter.defaultCenter().postNotificationName("UserAuthenticated", object: self) } else { println("Unknown error retrieving user info") NSNotificationCenter.defaultCenter().postNotificationName("UserCouldNotBeAuthenticated", object: self) } }) } }
ios swift
Mike taverne
source share