I am trying to create MVVM architecture with unit testing. I create MVVM architecture by flowing this link.
How I have to write unit testing for this I am very new to unit testing, see the code give below.
viewcontroller:
func initViewModel(){
//code for updating activity indicator in the view.
loginViewModelObj.updateLoadingStatus = { [weak self] () in
DispatchQueue.main.async {
let isLoading = self?.loginViewModelObj.isLoading ?? false
if isLoading {
Utils.showCustomisedActivityIndicator((self?.view)!, height: 35, width: 35)
}else {
Utils.HideActivityIndicator((self?.view)!)
}
}
}
loginViewModelObj.checkLoginType = { [weak self] () in
DispatchQueue.main.async {
let LoginType = (self?.loginViewModelObj.shouldCheckLoginType)!
if (LoginType == 0)
{
self?.verifyEmailView.isHidden = true
self?.verifyPasswordView.isHidden = false
self?.verifiedIdTextField.text = self?.loginViewModelObj.email
self?.passwordTextField.text = ""
}
}
}
}
I am calling this in viewdidload
and api calling is done under UIButton
:
let paramDict = ["user_input":userIdTextField.text!] as [String : Any]
let siginDict = ["variables" : paramDict]
//call api method in the ViewModel
loginViewModelObj.makeApiCall(paramsDict: siginDict as [String : AnyObject])
in view model, I am parsing the response using kvc and in view model I am doing the following code:
class LoginViewModel: NSObject {
var deafults = RewardsUserDefaults()
var checkLoginType : (() -> ())?
var shouldCheckLoginType :Int = -1
{
didSet {
self.checkLoginType?()
}
}
override init() {
super.init()
}
func makeApiCall(paramsDict : [String:AnyObject]){
self.isLoading = true
ApiCallManager.sharedManager.CallApiWithMethod(suffixUrl: “XXXXXXX", methodType: .post, joseToken: "", parameters: paramsDict as Dictionary<String,Any> as Dictionary<String, AnyObject>) { (result, error) in
self.isLoading = false
if error! == APICallsError.RequestSuccess {
if let dataDict = result!["data"] as? [String:AnyObject]{
if let userIdentityDict = dataDict["validateUserIdentity"] as? [String:AnyObject]{
if userIdentityDict["success"] as? Int == 1 {
let successObj = (LoginModel(response: Utils.getResultDictionary(result: result, error: error)))
self.email = successObj.email
self.shouldCheckLoginType = successObj.userLoginType
UserDefaults.standard.setValue(successObj.auth_token, forKey: "at")
return
}
}
}else{
print("API Call failed")
}
}
}
This the correct way of creating MMVM architecture, if yes how to apply unit testing for viewmodel, else if it is wrong can please suggest when I did the wrong.
Aucun commentaire:
Enregistrer un commentaire