我一直在寻找如何将ViewModel注入到测试中,以便能够对其进行测试。让我们假设视图模型有一个带有业务逻辑交互器的构造函数注入。我可以很容易地把它注射到片段中,但在测试中没有成功。
@HiltAndroidTest
class ViewModelTest
val randomViewmodel: RandomViewmodel// now what ? since by viewModels() is not accessible in tests
@Test
fun viewModelTet() {
randomViewmodel.triggerAct
我一直在努力解决我的问题,但我什么也找不到.
MyApp.kt:
@HiltAndroidApp
class MyApp: Application()
MainActivity.kt
@AndroidEntryPoint
class MainActivity: AppCompatActivity()
MyFragment.kt
class MyFragment: Fragment() {
private val myViewModel: MyViewModel by viewModels()
}
MyViewModel.kt
class MyViewModel @ViewModelI
我想将我的ViewModel内部片段与FragmentComponent依赖项一起使用,但我认为@ViewModelInject提供了ActivityComponent或ApplicationCoinmponent内部的ViewModel,不能将我的FragmentComponent依赖项注入ViewModel class XViewModel @ViewModelInject constructor(
private val xClass: XClass // this dependency provides in FragmentComponent
) : ViewModel()
我在我的片段中有一个EditText,我希望双向绑定EditText的文本值和viewModel变量,这样我就可以在viewModel中得到这个文本值来做一些额外的工作。
ViewModel:
class MyViewModel @ViewModelInject constructor(
private val myRepository: MyRepository,
private val myPreferences: MyPreferences
) : ViewModel() {
val name = myPreferences.getStoredName()
我有2 viewModels -
MainViewModel**StorageViewModel
StorageViewModel.kt
class StorageViewModel @ViewModelInject constructor(private val preferenceStorage:
PreferenceStorage, @ApplicationContext context: Context) : ViewModel() {
........
//save last played song
fun saveLastPlayedSong(song: S
我使用的是ASP.NET MVC4,我想加载大量数据,使其在我所有的类中都可以访问。以下是我的代码的极简主义示例:
public class MyController : Controller
{
public static List<MyObject> myList = null;
public ActionResult Index()
{
MyViewModel model = new MyViewModel();
myList = GetAllData(User.IDentity.Name); // Fill my l
在中,我们看到了一个MainCoroutineScopeRule。,它解释了除了Dispatchers.Main之外,这个规则还可以扩展到其他调度程序。
override fun starting(description: Description?) {
super.starting(description)
// If your codebase allows the injection of other dispatchers like
// Dispatchers.Default and Dispatchers.IO, consider injecting all
MVVM的优点之一是ViewModel的可测试性。在我的特殊情况下,我有一个VM,它在调用命令时加载一些数据,并进行相应的测试:
public class MyViewModel
{
public DelegateCommand LoadDataCommand { get; set; }
private List<Data> myData;
public List<Data> MyData
{
get { return myData; }
set { myData = value; RaiseProper
我第一次尝试安卓ViewModel和Hilt DI 我从下面的链接中了解到,要在运行时使用值初始化ViewModel,我应该使用ViewModelFactory Use a ViewModelFactory //ViewModel
class ScoreViewModel(finalScore: Int) : ViewModel() {
// The final score
var score = finalScore
init {
Log.i("ScoreViewModel", "Final score is $finalScore&
我尝试用DI创建一个使用MVVM + Repository模式的注册,我使用了@ViewModelInject,一切都正常,但是现在@ViewModelInject不再受欢迎,我将@ViewModelInject更改为@HiltViewModel + @Inject构造函数(),并面对error: [Dagger/MissingBinding] *.AuthRepository cannot be provided without an @Provides-annotated method.,我试图在接口中为寄存器函数添加一个@Provides注释,但是遇到了另一个错误。
任务执行失败:ap
我得到了无处不在的“对象引用”错误,不知道如何解决它。我相信这与调用局部视图有关。我使用的是jquery向导,因此局部视图是向导中显示的“步骤”。
在我的主.cshtml视图中,我这样做(省略了超文本标记语言):
@using MyNamespace.Models
@using MyNamespace.ViewModels
@model MyViewModel
...
...
using (Html.BeginForm())
{
...
// this works inside MAIN view (at least it goes through
// before
我正面临这个问题,在多模块android项目与刀柄。
kotlin.UninitializedPropertyAccessException: lateinit property repository has not been initialized in MyViewModel
我的模块是
ModuleViewmodel ModuleDataSource 应用程序模块UseCase
‘应用模块’
@AndroidEntryPoint
class MainFragment : Fragment() {
private lateinit var viewModel: MainViewModel
我尝试将依赖项注入到控制器中。我试着通过一个数组传递依赖项,通过一个内联匿名函数,并使用controller.$inject。什么都不管用,我已经准备好要爆炸了。
大多数提到依赖注入的博客和文章都显示了一个简单的示例,其中代码传递了$scope和一个依赖...但是,当我尝试复制代码示例时,什么都不起作用。
我遗漏了一些东西。我是否必须实例化依赖关系,或者如何传递依赖关系?
var myApp = angular.module('myApp', []);
function MyViewModel() {
this.theRightAnswer = 'The an
我有一个有两个活动的应用程序。Activity1更改存储在ViewModel中的State,然后启动Activity2。但在某种程度上,状态变化只反映在Activity1中,而不是在Activity2中。
ViewModel
class MyViewModel : ViewModel() {
var hasChanged: Boolean by mutableStateOf(false)
}
可合成的Activity1
@Composable
fun Screen1() {
val context = LocalContext.current
val viewMode
我是Xamarin.Forms新手,我想用MVVM模式和XAML创建一个跨平台的应用程序。在我的表单项目(pcl)中,我希望将我的BindingContext of MainPage以及将来的多个页面设置为相同的ViewModel。这个是可能的吗?让我展示一下我在说什么。下面是我以前的一个WPF项目(App.xaml.cs)的代码片段:
public partial class App : Application
{
private MainWindow _MainWindow;
private MyViewModel _ViewModel;
public App()
我继承了一个WPF MVVM应用程序,它几乎没有单元测试。当我试图编写自己的代码时,我很快意识到了原因:代码的编写方式使测试变得很容易,并且有很多具体的实现。
一个特别的问题是存储库。下面有一个EF上下文,我不能/不想测试它,但我甚至很难让它接受EF对象的Mocks。
我会举起手说:我不是一个很好的建筑师。你能帮我解开这一点吗,并促使我到哪里的接口和这样的需要添加?
下面是应用程序中的一个示例ViewModel:
public class MyViewModel : BaseViewModel
{
public MyViewModel(MyEFObject mEfObj)
{