You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R包开发:mockery与stubthat包的差异咨询

Comparing mockery and stubthat for R Package Stubbing

Great question! I’ve used both packages extensively for speeding up R package tests and cutting down memory overhead, so let me break down their key differences based on hands-on experience:

Core API & Usability

  • mockery: Takes a flexible, low-level approach. You’ll work with explicit mock() objects, use stub() to swap out target functions, and leverage helpers like expect_called() or expect_args() to verify exactly how dependencies were used. It’s perfect if you want fine-grained control over mock behavior and interaction tracking.
  • stubthat: Follows a more opinionated, high-level philosophy. Its single stub() function lets you replace a function with a fixed return value in one line—no need to predefine a mock object. This makes it super beginner-friendly for straightforward stubbing tasks where you just need to bypass expensive operations.

Feature Depth

  • mockery: Goes far beyond basic stubbing. You can:
    • Track call counts and inspect arguments passed to mocks
    • Set up conditional return values (e.g., return 5 on the first call, 10 on the second)
    • Make mocks throw errors to test error-handling logic
    • Verify that a mock was never called
      This makes it ideal for complex testing scenarios where you need to validate not just that a dependency was used, but how.
  • stubthat: Focuses on minimal, lightweight stubbing. It excels at replacing slow functions (like database queries or API calls) with static outputs to cut test time, but lacks advanced verification features. If you don’t need to check interactions, just replace the function, it’s a lean choice.

Community & Maintenance

  • mockery: As you noted, it’s been around longer and has a larger user base. You’ll find more tutorials, example code in popular R packages, and community support when troubleshooting. It’s also actively maintained, with regular updates to fix bugs and align with new R versions.
  • stubthat: A newer, more niche alternative. While it’s stable and functional, the community is smaller, so you might find fewer resources for edge cases. Its smaller codebase is a plus if you want to minimize dependencies in your package.

Performance Overhead

  • mockery: Since it tracks detailed metadata (call logs, argument lists), it has a tiny bit more overhead compared to stubthat. For most test suites, this difference is unnoticeable, but if you’re stubbing hundreds of functions in a single run, stubthat might be slightly more efficient in terms of memory and speed.
  • Both: Either way, using either package will drastically reduce test time and memory usage compared to running the original expensive functions—so you’re making a good choice either way.

Which Should You Choose?

  • Pick mockery if: You need to verify interactions with dependencies, set up dynamic mock behavior, or work in a complex test suite that requires robust mock controls.
  • Pick stubthat if: You just need to replace slow/memory-heavy functions with static values, and you prefer a simple, concise API without extra features.

Pro tip: You don’t have to pick just one! I’ve used both in the same test suite—stubthat for quick, simple stubs and mockery for more complex interaction verification. They play nicely together.

内容的提问来源于stack exchange,提问作者kjohnsen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:55:49