我正在尝试对一个依赖于另一个类的方法进行单元测试.该方法在该类上调用一个类方法,基本上是这样的:
func myMethod() {
//do stuff
TheirClass.someClassMethod()
}
Run Code Online (Sandbox Code Playgroud)
使用依赖注入技术,我希望能够用模拟替换"TheyClass",但我无法弄清楚如何做到这一点.有没有办法传入模拟类(不是实例)?
编辑:感谢您的回复.也许我应该提供更多细节.我试图模拟的类方法是在一个开源库中.
以下是我的方法.我试图测试它,同时嘲笑呼叫NXOAuth2Request.performMethod.此类方法发出网络调用以从后端获取经过身份验证的用户的信息.在闭包中,我将此信息保存到开源库提供的全局帐户存储中,并发布成功或失败的通知.
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, …Run Code Online (Sandbox Code Playgroud)