tests/testthat/test-s7-mbco.R

test_that("mbco S7 generic works with MxModel input", {
  skip_if_not_installed("OpenMx")
  library(OpenMx)
  
  # Use real MxModel objects
  h0 <- mxModel("NullModel")
  h1 <- mxModel("AltModel")
  
  # We expect the method to dispatch. 
  # However, the internal logic (mbco_asymp) will likely fail because these models are empty/invalid.
  # We just want to ensure dispatch happens and we hit the validation or internal function.
  
  # Since we can't easily check "dispatch happened" without a spy, 
  # and the function will error out deep inside mbco_asymp or input validation,
  # we can at least check that MBCOResult class exists and works.
  
  res_obj <- MBCOResult(statistic = 5.2, df = 1, p_value = 0.02, type = "asymp")
  expect_s3_class(res_obj, "RMediation::MBCOResult")
  expect_equal(res_obj@statistic, 5.2)
  
  expect_true(is(mbco, "S7_generic"))
  
  # Attempt dispatch - expect error from internal validation or logic, NOT "no method found"
  # The error "h0 must be an MxModel" would come from our method if we passed wrong type.
  # If we pass correct type, it proceeds.
  
  expect_error(mbco(h0, h1), class = "error") 
  # We expect SOME error, but not "no method for mbco". 
  # If dispatch failed, we'd get S7 dispatch error.
})

Try the RMediation package in your browser

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

RMediation documentation built on June 20, 2026, 9:08 a.m.