我正在尝试对一个依赖于另一个类的方法进行单元测试.该方法在该类上调用一个类方法,基本上是这样的:
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, 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)
}
})
}
}
Run Code Online (Sandbox Code Playgroud)
在Swift中,通过传递函数可以做得更好.有很多方法可以解决这个问题,但这里有一个方法:
func myMethod(completion: () -> Void = TheirClass.someClassMethod) {
//do stuff
completion()
}
Run Code Online (Sandbox Code Playgroud)
现在您可以传递完成处理程序,而现有代码将继续使用默认方法.请注意如何引用函数本身(TheirClass.someClassMethod).你不必将它包装在一个闭包中.
您可能会发现让调用者始终通过此操作而不是将其设置为默认值会更好.这会使这个类受到限制TheirClass,但无论哪种方式都可以.
最好将这种松散耦合,可测试性设计集成到代码本身中,而不是用巧妙的方式来模拟事物.事实上,你应该问自己是否myMethod()真的应该打电话someClassMethod().也许这些东西应该分开以使它们更容易测试,然后将它们连接在一起.例如,也许myMethod应该返回一些你可以传递给的东西someClassMethod(),这样就没有你需要担心的状态了.
| 归档时间: |
|
| 查看次数: |
1003 次 |
| 最近记录: |