tests/testthat/test-contracts.R

# Tests ---------------------------------------------------------

testthat::test_that('contracts() returns non-empty data.frame', {
  testthat::skip_if_offline()
  test <- contracts()
  testthat::expect_true(is.data.frame(test) && nrow(test) > 0)
  testthat::expect_false(any(c('twoYearCash', 'threeYearCash') %in% names(test)))
})

# Run contracts tests.
testthat::test_that('contracts() returns matched player name and position from players()', {
  ns <- asNamespace('nhlscraper')
  old_locked <- bindingIsLocked('.contracts_base', ns)
  if (old_locked) {
    unlockBinding('.contracts_base', ns)
  }
  old_contracts_base <- get('.contracts_base', envir = ns, inherits = FALSE)
  assign(
    '.contracts_base',
    data.frame(
      playerFullName = 'Jon Example',
      positionCode = 'LW',
      teamId = 5L,
      signedWithTeamId = 5L,
      signedWithTriCode = 'ABC',
      ageAtSigning = 25L,
      startSeasonId = 20242025L,
      endSeasonId = 20252026L,
      contractYears = 2L,
      contractAAV = 1.5,
      contractValue = 3,
      signingBonus = 0.5,
      stringsAsFactors = FALSE
    ),
    envir = ns
  )
  on.exit({
    assign('.contracts_base', old_contracts_base, envir = ns)
    if (old_locked) {
      lockBinding('.contracts_base', ns)
    }
  }, add = TRUE)
  testthat::local_mocked_bindings(
    players = function() {
      data.frame(
        playerId = 42L,
        playerFullName = 'Jonathan Example',
        playerFirstName = 'Jon',
        playerLastName = 'Example',
        positionCode = 'L',
        birthDate = '1999-07-01',
        currentTeamId = 5L,
        careerTeamId = 5L,
        firstSignedByTeamId = 5L,
        lastNHLTeamId = 5L,
        onRoster = 'Y',
        stringsAsFactors = FALSE
      )
    },
    .package = 'nhlscraper'
  )
  test <- get('contracts', envir = ns)()
  testthat::expect_equal(test$playerId, 42L)
  testthat::expect_equal(test$playerFullName, 'Jonathan Example')
  testthat::expect_equal(test$positionCode, 'L')
})

Try the nhlscraper package in your browser

Any scripts or data that you put into this service are public.

nhlscraper documentation built on June 29, 2026, 1:06 a.m.