百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

rspec-fire

> 编程语言
开源

已过时 - 在 RSpec 3 中使用验证双重

359 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

已过时 - 在 RSpec 3 中使用验证双重

TOTALLY OBSOLETE

RSpec fire has been completely subsumed by the verifying doubles feature in RSpec 3, which uses the same API. It's not just a port, the RSpec 3 ones are strictly better.

Leaving this here for posterity, but patches will not be accepted and there will be no further releases.

rspec-fire

Checks that stubbed methods on your test double exist, but still allow you to run in isolation when you choose. A failure will be triggered if an invalid method is being stubbed.

Once,
a younger brother came to him,
and asked,

"Father,
I have made and kept my little rule,
my fast,
my meditation and silence.
I strived to cleanse my heart of thoughts,
what more must I do?"

The elder rose up and,
stretched out his hands,
his fingers became like ten lamps ablaze.
He said,

"Why not be totally changed into fire?"

  -- Desert Way, Charlie Hunter

Test doubles are sweet for isolating your unit tests, but we lost something in the translation from typed languages. Ruby doesn't have a compiler that can verify the contracts being mocked out are indeed legit. This hurts larger refactorings, since you can totally change a collaborator --- renaming methods, changing the number of arguments --- and all the mocks that were standing in for it will keep pretending everything is ok.

rspec-fire mitigates that problem, with very little change to your existing coding style.

One solution would be to disallow stubbing of methods that don't exist. This is what mocha does with its Mocha::Configuration.prevent(:stubbing_non_existent_method) option. The downside is, you now have to load the collaborators/dependencies that you are mocking, which kind of defeats the purpose of isolated testing. Not ideal.

Another solution, that rspec-fire adopts, is a more relaxed version that only checks that the methods exist if the doubled class has already been loaded. No checking will happen when running the spec in isolation, but when run in the context of the full app (either as a full spec run or by explicitly preloading collaborators on the command line) a failure will be triggered if an invalid method is being stubbed.

Usage

It's a gem!

gem install rspec-fire

Bit of setup in your spec_helper.rb:

require 'rspec/fire'

RSpec.configure do |config|
  config.include(RSpec::Fire)
end

Specify the class being doubled in your specs:

class User  true)

# ...or give it a list of constants to transfer
class_double("MyCoolGem").as_stubbed_const(:transfer_nested_constants => [:Widget])

Doubling class methods

Particularly handy for ActiveRecord finders. Use class_double. If you dig into the code, you'll find you can create subclasses of FireDouble to check for any set of methods.

Preventing Typo'd Constant Names

instance_double("MyClas") will not verify any mocked methods, even when MyClass is loaded, because of the typo in the constant name. There's an option to help prevent these sorts of fat-finger errors:

RSpec::Fire.configure do |config|
  config.verify_constant_names = true
end

When this is set to true, rspec-fire will raise an error when given the name of an undefined constant. You probably only want to set this when running your entire test suite, with all production code loaded. Setting this for an isolated unit test will prevent you from being able to isolate it!

Mocking Done Right (tm)

  • Only mock methods on collaborators, not the class under test.
  • Only mock public methods.

If you can't meet these criteria, your object is probably violating SOLID principles and you should either refactor or use a non-isolated test.

Compatibility

Only RSpec 2.11+ is supported. Tested on all the rubies thanks to Travis CI.

Developing

bash
git clone https://github.com/xaviershay/rspec-fire.git
bundle install
bundle exec rake spec

Patches welcome! I won't merge anything that isn't spec'ed, but I can help you out with that if you are getting stuck.

Still need to support #stub_chain.

Status

rspec-fire is pretty new and not used widely. Yet.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言