✅ unit testing ViemModels

I am very new to unit testing, and I have a typical example where I have a constructor with 4 services 3 of which is DI and one concrete generic for navigation E.g public
cs class ViewModel {

Public ViewModel ( IB service, IC service, ID service, E<F> navigation) {

}

}
cs class ViewModel {

Public ViewModel ( IB service, IC service, ID service, E<F> navigation) {

}

}
I have be having problem testing because I need to create a moq of E<F> and because of the way it is setup it's hard to abstract to interface. Does it make sense to create IViewModel And then have Class ViewModel:IViewModel And mock IViewModel instead? Is this the recommended approach? Is this good or bad ?
6 Replies
cap5lut
cap5lut15mo ago
u cant mock the view model if u want to test the view model u will have to mock the services and the navigation.
LPeter1997
LPeter199715mo ago
I'd honestly never test a VM, only the services themselves if they belong to the same codebase ofc
cap5lut
cap5lut15mo ago
question is why its hard to make an interface for navigation
LPeter1997
LPeter199715mo ago
There is not a lot you can/should be testing in the VM directly, ideally it's just an adaptation layer that might change a lot depending on the UI, so it might actually be disadvantageous to test it
from I select q[0]
from I select q[0]OP15mo ago
@LPeter1997 @cap5lut it's hard because of the way the MVVM navigation is setup based on singletonSeans videos I will post a link to a GitHub, I have showing the navigation snippet I have no issues mocking the services as theres proper DI in place and constructor injection but can't for the navigation as it's concrete
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server