junit 单元测试安排无效

nlejzf6q  于 2023-05-17  发布在  其他
关注(0)|答案(1)|浏览(222)

我已经将isGpsEnabled设置为false,但是当我调试测试时,它仍然显示为true。我不知道为什么会这样。

@Before
    fun setupViewModel() {
        preferences = FakePreferences()
        connectivityObserver = FakeConnectivityObserver()
        locationRepository = FakeLocationRepository()
        weatherRepository = FakeWeatherRepository()
        weatherUseCase = WeatherUseCases(
            getAllWeather = GetAllWeather(weatherRepository),
            convertUnit = ConvertUnit(),
        )
        locationUseCases = LocationUseCases(
            ValidateCurrentLocation(locationRepository, preferences),
        )

        weatherInfoViewModel = WeatherInfoViewModel(
            appPreferences = preferences,
            connectivityObserver = connectivityObserver,
            locationUseCases = locationUseCases,
            weatherUseCases = weatherUseCase,
            locationRepository = locationRepository,
        )
    }
@Test
    fun firstTimeRunAppAndGrantLocationPermissionButLocationServiceDisabled_navigateToSearch() =
        runTest {
            // Arrange
            locationRepository.isGpsEnabled = false
            assertTrue(weatherInfoViewModel.isInitializing.value)
            weatherInfoViewModel.onFirstTimeLocationPermissionResult(true)
            runCurrent()
            assertEquals(
                WeatherDestinations.SEARCH_ROUTE,
                weatherInfoViewModel.appRoute,
            )
            assertFalse(weatherInfoViewModel.isInitializing.value)
        }

rbpvctlc

rbpvctlc1#

是调试问题;尝试在FakeRepository中创建isGpsEnabled = false,并重新检查您的调试断点,如果它仍然为true,则意味着其他东西正在更改此值,因此请检查其他可能访问此值的位置并更改。

其他可能的问题:

  • 检查isGpsEnabled属性是否在代码中的其他位置被重写。可以尝试搜索正在设置或访问该属性的任何其他示例。
  • 正如Logain所说,确保您在测试中使用的locationRepository示例与WeatherInfoViewModel使用的示例相同。创建对象的多个副本时,对一个副本的修改可能不会出现在其他副本中。

如果这些解决方案都不起作用,请与我们分享源代码,以便我们可以在我们的终端调试它🙏!

相关问题