覆盖单元测试中的匿名函数参数

时间:2017-07-11 13:46:02

标签: javascript react-native mocha chai jest

我在代码中有多种情况,我们在创建对象时将函数作为参数传递(就像在onPress中一样):

<Touchable
  onPress={() => Linking.openURL(formatUrl(url))} 
  noContainer={true}>      
    {children}
</Touchable>

当涉及到它时,这是一个更大的组件得到渲染的片段。由于它实际上并不是组件的一种方法,我对这里的测试并不特别感兴趣 - 如果需要,它将作为另一个组件的一部分进行测试。 Code Coverage 但是,代码覆盖率报告返回时显示该功能未被发现。

有没有办法满足这个范围 - 要么通过测试这个函数,要么忽略以这种方式传递的所有函数(匿名,作为参数)?

1 个答案:

答案 0 :(得分:0)

假设您正在使用Jest,则可以将Jest模拟设置为调用它接收的参数,因此:

if

因此,在这种情况下,模拟的# Create tibble / data frame df <- tibble::tibble("Unit" = c("A","B","C"), "1990" = c(80,50,90), "1991" = c(75,40,90), "1992" = c(45,0,89), "1994" = c(0,0,87), "1995" = c(0,0,0)) # Transform from wide to long format # and add an index per unit df_g <- df %>% tidyr::gather(key = "year", value = "val", 2:6) %>% dplyr::arrange(Unit, year) %>% dplyr::group_by(Unit) %>% dplyr::mutate(.index = 1 : dplyr::n()) df_g # # A tibble: 15 x 4 # # Groups: Unit [3] # Unit year val .index # <chr> <chr> <dbl> <int> # 1 A 1990 80 1 # 2 A 1991 75 2 # 3 A 1992 45 3 # 4 A 1994 0 4 # 5 A 1995 0 5 # 6 B 1990 50 1 # 7 B 1991 40 2 # 8 B 1992 0 3 # 9 B 1994 0 4 # 10 B 1995 0 5 # 11 C 1990 90 1 # 12 C 1991 90 2 # 13 C 1992 89 3 # 14 C 1994 87 4 # 15 C 1995 0 5 # Identify the first year per unit with the value 0 zeroes <- df_g %>% dplyr::filter(val == 0) %>% dplyr::group_by(Unit) %>% dplyr::filter(dplyr::row_number() == 1) %>% dplyr::select(-c(year, val)) %>% dplyr::rename(zero = .index) zeroes # # A tibble: 3 x 2 # # Groups: Unit [3] # Unit zero # <chr> <int> # 1 A 4 # 2 B 3 # 3 C 5 # Add that information with a join operation # and create the new column names df_z <- df_g %>% dplyr::left_join(zeroes, by="Unit") %>% dplyr::mutate(step = .index - zero, new_name = paste0("X", ifelse(step >= 0, "+", "-"), abs(step))) %>% dplyr::select(Unit, new_name, val) df_z # # A tibble: 15 x 3 # # Groups: Unit [3] # Unit new_name val # <chr> <chr> <dbl> # 1 A X-3 80 # 2 A X-2 75 # 3 A X-1 45 # 4 A X+0 0 # 5 A X+1 0 # 6 B X-2 50 # 7 B X-1 40 # 8 B X+0 0 # 9 B X+1 0 # 10 B X+2 0 # 11 C X-4 90 # 12 C X-3 90 # 13 C X-2 89 # 14 C X-1 87 # 15 C X+0 0 # Spread to wide format again df_transformed <- df_z %>% tidyr::spread(key = "new_name", value = "val") df_transformed # # A tibble: 3 x 8 # # Groups: Unit [3] # Unit `X-1` `X-2` `X-3` `X-4` `X+0` `X+1` `X+2` # <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> # 1 A 45 75 80 NA 0 0 NA # 2 B 40 50 NA NA 0 0 0 # 3 C 87 89 90 90 0 NA NA 函数将收到您的匿名函数并对其进行调用。覆盖范围将显示匿名函数已被调用。

希望它会有所帮助:)